Link to home
Start Free TrialLog in
Avatar of Mensana
Mensana

asked on

enum 2 int back 2 enum


Hi Experts,

I have an enum type declared like this:

enum _en_Monts
{
   January = 1,
   February,
   March,
   April,
   May,
   June,
   July,
   August,
   September,
   October,
   November,
   December
};

Then I have this piece of code:

_en_Monts aMonth = June, anotherMonth = August;

long ulMonth = aMonth;

anotherMonth = *(reinterpret_cast<_en_Monts *>(&ulMonth));

MSDN claims that the last sentence is correct but the result is undefined. Is this the real case?

PS: Here is the example from MSDN

// Example of the enum keyword
enum Days               // Declare enum type Days
{
   saturday,            // saturday = 0 by default
   sunday = 0,          // sunday = 0 as well
   monday,              // monday = 1
   tuesday,             // tuesday = 2
   wednesday,           // etc.
   thursday,
   friday
} today;                // Variable today has type Days

int  tuesday;           // Error, redefinition of tuesday

enum Days yesterday;    // Legal in C and C++
Days tomorrow;          // Legal in C++ only

yesterday = monday;

int i = tuesday;        // Legal; i = 2
yesterday = 0;          // Error; no conversion
yesterday = (Days)0;    // Legal, but results undefined

// end of sample

This last line intrigues me. I run my code and it worked, but can I be sure that it always will?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Pavlik
Pavlik

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