size of a class having enumerated data types?

Discussion in 'C++' started by ypjofficial@indiatimes.com, Jan 23, 2006.

  1. Guest

    Hi all,

    In what way does the enumerated data type contibute to the size of a
    class if its part of that class?

    eg.
    #include <iostream.h>
    class one
    {
    public:
    enum months{jan =1,feb =2};//till dec = 12
    };

    int main()
    {
    cout<<sizeof(one);//outputs 1
    cout<<sizeof(one::months);//outputs 4
    }

    so why the size of enumerated datatype months doesn't get contributed
    to the size of the one class?

    Thanks and Regards,
    Yogesh Joshi


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    , Jan 23, 2006
    #1
    1. Advertising

  2. John Carson Guest

    <> wrote in message
    news:
    > Hi all,
    >
    > In what way does the enumerated data type contibute to the size of a
    > class if its part of that class?


    The mere definition of a data type never contributes to the size of a class.

    > eg.
    > #include <iostream.h>
    > class one
    > {
    > public:
    > enum months{jan =1,feb =2};//till dec = 12
    > };
    >
    > int main()
    > {
    > cout<<sizeof(one);//outputs 1
    > cout<<sizeof(one::months);//outputs 4
    > }
    >
    > so why the size of enumerated datatype months doesn't get contributed
    > to the size of the one class?


    Consider:

    #include <iostream>
    using namespace std;

    class two
    {
    public:
    class inner
    {
    int x;
    };
    };

    int main()
    {
    cout << sizeof(two) << endl; //outputs 1
    cout << sizeof(two::inner) << endl; // outputs 4

    return 0;
    }

    If you want a contribution to the size of the class, then you need to
    declare an object of the data type that you have defined, e.g.,

    class one
    {
    public:
    enum months{jan =1,feb =2};
    months object;
    };


    class two
    {
    public:
    class inner
    {
    int x;
    };
    inner object;
    };


    int main()
    {
    cout << sizeof(one) << endl; //outputs 4
    cout << sizeof(two) << endl; //outputs 4

    return 0;
    }


    --
    John Carson
     
    John Carson, Jan 23, 2006
    #2
    1. Advertising

  3. Heinz Ozwirk Guest

    <> schrieb im Newsbeitrag
    news:...
    ....
    > class one
    > {
    > public:
    > enum months{jan =1,feb =2};//till dec = 12
    > };
    > int main()
    > {
    > cout<<sizeof(one);//outputs 1


    This prints the size of an instance of class one. Since that class does not
    does not define any data members, the size of an object of that type is 1.

    > cout<<sizeof(one::months);//outputs 4


    This prints the size of an instance of one::months. And since that is an
    enumaration type, the size of its members is, IIRC, unspecified.

    > }
    >
    > so why the size of enumerated datatype months doesn't get contributed
    > to the size of the one class?


    A type has no size. Only objects have a size, and a type is not an object.
    Yes, you can write sizeof(type), but that does return the size of an object
    of the specified type.

    HTH
    Heinz



    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Heinz Ozwirk, Jan 23, 2006
    #3
  4. Guest

    Hello Yogesh,

    In your class enum is declared as one type. And it is not instantiated.
    On instantiating the object of this class there will be no member
    variable of type enum months in the object. Therefore the sizeof this
    class will only show the size of the empty class.

    But if you create a member variable of type enum months in your class
    then it will include the enums size in your class size....

    class one
    {
    public:
    enum months{jan =1,feb =2} m_variable; //till dec = 12

    };


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    , Jan 23, 2006
    #4
  5. Jim Langston Guest

    <> wrote in message
    news:...
    > Hi all,
    >
    > In what way does the enumerated data type contibute to the size of a
    > class if its part of that class?
    >
    > eg.
    > #include <iostream.h>
    > class one
    > {
    > public:
    > enum months{jan =1,feb =2};//till dec = 12
    > };
    >
    > int main()
    > {
    > cout<<sizeof(one);//outputs 1
    > cout<<sizeof(one::months);//outputs 4
    > }
    >
    > so why the size of enumerated datatype months doesn't get contributed
    > to the size of the one class?
    >
    > Thanks and Regards,
    > Yogesh Joshi


    Because you only declared the enum months, you never instantized it.

    change it to:
    class one
    {
    public:
    enum months { jan = 1, feb = 2 } mymonths;
    };

    or
    enum months { jan = 1, feb = 2 };
    months mymonths;

    Now do a sizeof(one) and it'll be 4.

    enum months { jan }; only declares months, it doesn't create a variable
    called months.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Jim Langston, Jan 23, 2006
    #5
  6. Rolf Magnus Guest

    wrote:

    > Hi all,
    >
    > In what way does the enumerated data type contibute to the size of a
    > class if its part of that class?
    >
    > eg.
    > #include <iostream.h>
    > class one
    > {
    > public:
    > enum months{jan =1,feb =2};//till dec = 12
    > };
    >
    > int main()
    > {
    > cout<<sizeof(one);//outputs 1
    > cout<<sizeof(one::months);//outputs 4
    > }
    >
    > so why the size of enumerated datatype months doesn't get contributed
    > to the size of the one class?


    Because it is - as you wrote above - a type. Types themselves don't need any
    memory (as far as the C++ standard is concerned). Only instances of them
    do, but there is no instance of your enum.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Rolf Magnus, Jan 23, 2006
    #6
  7. benben Guest

    wrote:
    > Hi all,
    >
    > In what way does the enumerated data type contibute to the size of a
    > class if its part of that class?
    >
    > eg.
    > #include <iostream.h>
    > class one
    > {
    > public:
    > enum months{jan =1,feb =2};//till dec = 12
    > };
    >
    > int main()
    > {
    > cout<<sizeof(one);//outputs 1
    > cout<<sizeof(one::months);//outputs 4
    > }
    >
    > so why the size of enumerated datatype months doesn't get contributed
    > to the size of the one class?


    That's because sizeof(one) calculates the size of an object of type one.
    one::months is a type not a data member therefore it does not grow one's
    size.

    To give an analogue, consider an equivalent:

    class one{};

    enum months {/*...*/};

    int main(){
    one o;
    cout << sizeof(a); // 1
    }

    The only difference is that now you refer one::month as just month.

    In case you are curious why an empty one class has a size (of 1), this
    is because it needs to take up a bit of memory so its memory address is
    unique.

    Ben


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    benben, Jan 23, 2006
    #7
  8. In article <>,
    writes
    >Hi all,
    >
    >In what way does the enumerated data type contibute to the size of a
    >class if its part of that class?
    >
    >eg.
    >#include <iostream.h>

    Warning: pre-standard header
    >class one
    >{
    >public:
    >enum months{jan =1,feb =2};//till dec = 12

    No need to assign values after the first one. However this is just a
    definition of a user defined type and so has zero size. Instances of the
    enum will have the size of whatever integer type the implementation uses
    (could be as small as a char in this case, but must nott be larger than
    an int)
    >};
    >
    >int main()
    >{
    >cout<<sizeof(one);//outputs 1

    As one has no data members it has the minimum allowed size for any type
    of 1.

    >cout<<sizeof(one::months);//outputs 4


    Which tells you how much storage is need for a variable of type
    one::months

    >}
    >
    >so why the size of enumerated datatype months doesn't get contributed
    >to the size of the one class?

    Because it isn't data member of the class (nor do functions contribute
    to the size of the class.


    --
    Francis Glassborow ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    For project ideas and contributions: http://www.spellen.org/youcandoit/projects


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Francis Glassborow, Jan 23, 2006
    #8
  9. You didn't actually declare a variable/instance of months type. All you
    did was declare an enumerated type called months. In fact you can
    typedef or declare a nested class/struct inside your class and as long
    as you don't declare any varible of of that type, the size of the class
    won't be affected. Your class 'one' is empty since you did not declare
    any variable. The size of any empty class is implementation depandant
    but always greater than zero.

    The size of any enum is also implementation dependant but usually
    either the size of a byte or an integer. It also depends on the largest
    size of an enumerated value.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Fhulu Lidzhade, Jan 23, 2006
    #9
    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. rfractal30
    Replies:
    6
    Views:
    499
    rfractal30
    Feb 25, 2005
  2. David R. Throop
    Replies:
    0
    Views:
    434
    David R. Throop
    May 6, 2005
  3. Kevin Neilson

    Assigning Values to Enumerated Types

    Kevin Neilson, May 7, 2008, in forum: VHDL
    Replies:
    9
    Views:
    612
    Kevin Neilson
    May 9, 2008
  4. Immortal Nephi
    Replies:
    4
    Views:
    420
    Öö Tiib
    Aug 24, 2010
  5. KevinSimonson

    Using Enumerated Types as Array Indexes

    KevinSimonson, Aug 16, 2011, in forum: Java
    Replies:
    51
    Views:
    2,012
    Andreas Leitgeb
    Aug 20, 2011
Loading...

Share This Page