I came across some unusual (to me at least...) behavior with C++'s enums. I've tried the following in Visual Studio 2008 and g++ version 4.4.3
#include <iostream>
using namespace std;
enum testEnum
{
// no zero enum
one = 1,
two = 2,
three = 3
};
int main(int argc, char *argv[])
{
testEnum e; // undefined value (may be zero, but thats just luck)
cout << "Uninitialized enum e = " << e << endl;
/*
testEnum e2(0); // error converting from int to enum
*/
testEnum e3(testEnum(0)); // forces zero !?!!?!?
cout << "zero enum e3 = " << e3 << endl; // prints '0'
testEnum e4(testEnum(9999)); // forces 9999 ?!?!?!?!
cout << "9999 enum e4 = " << e4 << endl; // prints '9999'
return 0;
}
The undefined value for e is as I expected, and I understand why you can't convert from an int to an enum (but you can go the other way).
I'm curious as to how the last two enums (e3 and e4) are allowed to compile and attain any value you want to give them.
Also, I found that this:
testEnum e();
Compiled in both studio and linux, and cout-ing in linux yielded '1' but in studio I got a linker error:
main.obj : error LNK2001: unresolved external symbol "enum testEnum __cdecl e2(void)"
In studio, I could do:
testEnum e = testEnum();
But cout-ing it yielded '0' not '1'
So my main question is how you can ram any value down an enum's throat like e3 and e4 in the above example. And if this is implementation dependent or not.