Solved

enum 2 int back 2 enum

Posted on 2002-04-10
1
351 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
designing in object programming 12 110
How do I get Window Title of all opened process? 4 39
what type of pointer is this pointer? 3 24
Bitwise and to sum elements 2 17
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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