|
-
November 21st, 2008, 07:25 AM
#1
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
-
November 21st, 2008, 08:18 AM
#2
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.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 21st, 2008, 09:06 AM
#3
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)
-
November 21st, 2008, 09:14 AM
#4
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
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
November 21st, 2008, 09:33 AM
#5
Re: size of enum
Thanks a lot
Now I am very much clear
-
November 21st, 2008, 11:50 AM
#6
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
-
November 22nd, 2008, 06:03 AM
#7
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|