Playing Undefined Behavior . The value of enummy is undefined. Conceptually, there is no difference between your code and the following code:
int main() { int i;
If you defined your variable in the namespace scope, this will be initialized to 0.
enum SomeEnum { EValue1 = 1, EValue2 = 4, }; SomeEnum e; // e is 0 int i; // i is 0 int main() { cout << e << " " << i; //prints 0 0 }
Do not be surprised that e may have values โโdifferent from any values โโof the SomeEnum enumerator. Each enumeration type has a basic integral type (for example, int , short or long ), and the set of possible values โโof an object of this enumeration type is a set of values โโthat has a basic integral type. Enum is just a way to conveniently name some of the values โโand create a new type, but you do not limit the values โโof your enumeration to the set of counter values.
Update: Some quotes supporting me:
For zero initialization of an object of type T means:
- if T is a scalar type (3.9), the object is set equal to 0 (zero), converted to T;
Note that enums are scalar types.
To initialize an object of type T means:
- if T - class type blah blah
- if T is a nonunit class, the type of blah blah
- if T is an array type, then blah blah - otherwise the object is initialized to zero
So, we get to another part. And the namespace scope objects are initialized with a value
Armen Tsirunyan Jul 27 '11 at 10:24 2011-07-27 10:24
source share