Solved

enum 2 int back 2 enum

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C Language combined operators 28 114
computer science syllabus 3 89
Error C2678: binary '!=': no operator found... 4 58
Embarcadero WebBroker REST server endpoint parameters 2 82
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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

860 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