Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

re enum type initialization

Posted on 1999-07-03
10
Medium Priority
?
261 Views
Last Modified: 2010-04-02
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
0
Comment
Question by:miredo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1199098
Garbage.
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1199099
This thing is pretty easy to check, why wasting 100 points?
0
 
LVL 3

Expert Comment

by:Try
ID: 1199100
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:Try
ID: 1199101
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.
0
 

Author Comment

by:miredo
ID: 1199102
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.
0
 
LVL 5

Accepted Solution

by:
yonat earned 400 total points
ID: 1199103
Thanks for the points!
0
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1199104
Well it's your call of course, but it takes exactly 10 lines of code and 2 minutes to test
0
 
LVL 3

Expert Comment

by:Try
ID: 1199105
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"!
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199106
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).
0
 

Author Comment

by:miredo
ID: 1199107
Sorry, Try, I misread your answer.  :-)

miredo
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question