-
size of enum
Hi,
When I create an enum like this
enum x {aaa,bbb,ccc};
cout<<sizeof(x)<<endl; shows the 4
if I put the enum in a class like this
class abc{
public:
enum a { aaa,bbb,ccc=102,dd};
};
and in the main I Print tha size of the object it should show me 4 again but it is showing 1
I think this is wrong. I am using .NET compiler
plz clarify
-
Re: size of enum
Your class has no data members, thus the size of any object of this class should theoretically be zero. However, C++ demands that objects have a size of at least 1, therefore 1 is what you get as a result.
abc::a is a type, not a data member in case you wonder.
-
Re: size of enum
but I have given index for one of them i.e for ccc.
Actually I have not used much enum and not clear with the concept
can you please explain some other related type as of enum so that if we declare it in a class then the size of the object becomes 1 (other than enum)
-
Re: size of enum
Here is another example:
You have Outer class with no members (empty), but it has inner type defined (GiganticInner). However, as you can see, it has no impact on size of outer class objects, as it has no data fields:
Code:
#include <iostream>
using namespace std;
class Outer {
class GiganticInner {
int array[100000];
};
public:
void printInnerSize() {
GiganticInner temp;
cout << "Size of object of inner class: " << sizeof(temp) << '\n';
}
};
int main() {
Outer outer;
cout << "Size of object of outer class: " << sizeof(outer) << '\n';
outer.printInnerSize();
}
My Outer is like your abc, and GiganticInner just like internal enym type definition.
Cheers
-
Re: size of enum
Thanks a lot
Now I am very much clear
-
Re: size of enum
Enums are just a collection constant integers.
Remember that enums are numbered starting with 0 unless you specify a number.
so:
Code:
enum a { aaa,bbb,ccc=102,dd}
is basically like doing:
Code:
#define aaa 0
#define bbb 1
#define ccc 102
#define dd 103
-
Re: size of enum
And another compiler may well make the the size of that enum 1 or even 2, since it only has to be big enough to hold the largest value, and yours would fit in a char.