Solved

enum 2 int back 2 enum

Posted on 2002-04-10
1
346 Views
Last Modified: 2010-04-02

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.
0
Comment
Question by:Mensana
1 Comment
 
LVL 1

Accepted Solution

by:
Pavlik earned 75 total points
ID: 6934709
According to the standart of C++ every enum has different type. This guarantees that you wont be able to convert Days to Colors for example.
You can promote enum value to the integral type. That's why "int i = tuesday;" works. you should only make sure that you variable of integral type is big enough.
For example:
================================
enum A
{
  A1,
  A2 = 65536
} a;


char c;

a = A2;
c = a;  // here c will be equal to 0 , not 65536
===============================
There is no implicit conversion from integral type to enum type. Otherwise you would be able to do something like this:
========================
Days d;
Colors c;
int i;
c = red;
i = c;
d = i; //at this point d equals tp red which is just meaningless
========================
But you can make explicit conversion like d = (Days)i;
In this case you should yourself make sure that value in i is one of the constants defined in Days. Compiler wont do it for you. That's why you have legal code but undefined result in both cases.
Consider for example how you program will act after assidnment like this:
=====================
Days d;
d = (Days)123456;
====================

Best regards,
Pavlik
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need 'Configuration File' Management Library for C++ Project 1 487
Least Squares Curve Fitting 4 61
Header of docx file 17 98
Issues with C++ Class 19 81
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now