size of an empty class

Discussion in 'C++' started by free2cric@yahoo.com, Aug 3, 2005.

  1. Guest

    Hi,
    What will the syntax sound like for checking size of an empty class?
    what will be the size.
    Thanks,
    Cric
     
    , Aug 3, 2005
    #1
    1. Advertising

  2. Srini Guest

    > Hi,
    > What will the syntax sound like for checking size of an empty class?
    > what will be the size.
    > Thanks,
    > Cric


    #include <iostream>

    class A
    {
    };

    int main()
    {
    std::cout << sizeof(A);
    return 0;
    }

    Srini
     
    Srini, Aug 3, 2005
    #2
    1. Advertising

  3. Ian Guest

    wrote:
    > Hi,
    > What will the syntax sound like for checking size of an empty class?
    > what will be the size.


    At least one byte.

    Ian
     
    Ian, Aug 3, 2005
    #3
  4. WittyGuy Guest

    Ian wrote:
    > At least one byte.


    What is the advantage of using empty class?

    -Wg-
     
    WittyGuy, Aug 3, 2005
    #4
  5. msalters Guest

    WittyGuy schreef:

    > Ian wrote:
    > > At least one byte.

    >
    > What is the advantage of using empty class?


    Empty classes do not have to take up space when used as base classes.
    E.g. an empty class deriving from two empty base classes can also be
    1 byte.

    The main reason "really" empty classes (i.e. nothing between the { }
    and
    no base classes ) are in the language is that it would take an extra
    rule
    to disallow them. That's not worth it

    HTH,
    Michiel Salters
     
    msalters, Aug 3, 2005
    #5
  6. WittyGuy Guest

    msalters wrote:
    > to disallow them. That's not worth it


    Do you mean that C++ fails to restrict this? In real-world empty class
    is not implemented?

    -Wg-
     
    WittyGuy, Aug 3, 2005
    #6
  7. Imre Palik Guest

    "WittyGuy" <> writes:

    > Ian wrote:
    > > At least one byte.

    >
    > What is the advantage of using empty class?


    No advantage. Either you need it & use it, or don't need it & don't
    use it.

    They are used oft with template metaprogramming or as abstract base
    classes.

    ImRe
     
    Imre Palik, Aug 11, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Carson
    Replies:
    10
    Views:
    21,060
    cplusprog
    Apr 18, 2010
  2. Alexander Terekhov

    Re: Why empty class size is 1 Byte ???

    Alexander Terekhov, Jul 7, 2003, in forum: C++
    Replies:
    0
    Views:
    424
    Alexander Terekhov
    Jul 7, 2003
  3. raghav
    Replies:
    0
    Views:
    681
    raghav
    Jul 7, 2003
  4. Sweety
    Replies:
    4
    Views:
    7,603
    rossum
    Aug 21, 2004
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
Loading...

Share This Page