Solved

enum 2 int back 2 enum

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

803 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