Link to home
Start Free TrialLog in
Avatar of miredo
miredo

asked on

re enum type initialization

I've defined an enum type,

enum MyEnumType
{
   MyEnumA,
   MyEnumB,
   MyEnumC
};


I when I use it in a class,

class MyClass
{
   MyEnumType membername;
   int anothermember;
   (more members...)
};

what will membername be initialized to when a MyClass object is created?

miredo
Avatar of yonat
yonat

Garbage.
This thing is pretty easy to check, why wasting 100 points?
Without knowing how you plan to initialize an object of MyClass, if you simply instantiate an object from MyClass without making any effort to initialize 'membername' (in the constructor, for example), 'membername' could be anything (including garbage).

You MUST first initialize 'membername' to either MyEnumA, MyEnumB, or MyEnumC for it to have any meaningful value.  After doing that, you can refer to 'membername' anywhere in your program (e.g. while(membername == MyEnumA) etc., or if(membername == MyEnumB) etc.) for it to have predictable results.

You simply initialize 'membername' as follow:

membername = MyEnumA;   // or any of the other enum constants
Unless you indicate a specific value for MyEnumA, it will automatically be assigned zero, as default, by the compiler.  MyEnumB will then be assigned 1, and MyEnumC will be assigned 2.  IOW, after the default value of 0 has been assigned by the compiler to the first identifier, each successive values of the enumeration constants will be incremented by 1.

This also means that you can specifically begin the first identifier with a value other than 0.  For example:

enum Months {Jan = 1, FEB, MAR, APR, MAY, etc.}

this way, each month of the year will correspond to its proper numneric value.

There are a number of clever things you can do with enum.
  If you have any other questions, go ahead and post them.
Avatar of miredo

ASKER

I cannot accept your answer, Try,  because I know, after asking this question in many places, that your answer is not right.  Yonat gave the right answer.  I will give the points, if Yonat wants to "propose an answer".



To MikeP, you ask:

"This thing is pretty easy to check, why wasting 100 points?"

Answer: because time is more precious than points.
ASKER CERTIFIED SOLUTION
Avatar of yonat
yonat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well it's your call of course, but it takes exactly 10 lines of code and 2 minutes to test
Interesting that you said my answer was not right.  I've used it countless amount of time and have NEVER had the compiler given me an error for it.

Yonat is correct of course, but I pretty much said the same thing in my first sentence that without knowing how you propose initializing the object (because you could initialize the enum variable in the constructor), it could be anything, "including garbage".

Yonat is a pretty good C++ programmer, and I don't mind him getting the points (after all, he did say "garbage" first), but coming from you, Miredo, or coming from the compiler, I'll take the compiler anyday, and it hasn't told me that I'm "not right"!
The value of the enum will be known if (and only if, I think) the class variable is a static.  In this case the enum will be initialised to zero, and you could rely on it (but not good practise).
Avatar of miredo

ASKER

Sorry, Try, I misread your answer.  :-)

miredo