473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is an enum?

Using VS C++.NET 2005 Express in clr:/pure syntax. I have this enum, defined
like so:

enum My_Enum
{
ENUM_0 = 0,
ENUM_1
} ;

My_Enum enum_var ;

I'm trying to save/load enum_var to/from a file using ostream/istream. But:

output << enum_var ; //ok
input >> enum_var ; //error

So I tried saving the 'int' value of the enum vaviable:

output << int(enum_var) ; // ok
input >> enum_var ; //error

and even this doesn't work:

output << int(enum_var) ; // ok

int value ;
input >> value ; // ok
enum_var = value ; // error, can't convert?!

Help!! : )

[==P==]
Dec 15 '05 #1
5 1064
Peteroid wrote:
int value ;
input >> value ; // ok
enum_var = value ; // error, can't convert?!


int doesn't convert to an enum. You have to cast it:

int value;
input >> value;
enum_var = static_cast<My_Enum>(value);

Tom
Dec 15 '05 #2
Hi Tom,

Thanks for the workaround! : )

[==P==]

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Peteroid wrote:
int value ;
input >> value ; // ok
enum_var = value ; // error, can't convert?!


int doesn't convert to an enum. You have to cast it:

int value;
input >> value;
enum_var = static_cast<My_Enum>(value);

Tom

Dec 15 '05 #3
>> int doesn't convert to an enum. You have to cast it:

Thanx for the workaraound, which I'm using! : )

However, how you explain this quote form an msdn2 on-line page:

http://msdn2.microsoft.com/en-us/library/17z041d4.aspx

The enum type
ANSI 3.5.2.2 The integer type chosen to represent the values of an
enumeration type
A variable declared as enum is an int.

So either they better take down this page or an enum variable IS an 'int',
let alone not being able to convert it to one! :)

[==P==]
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Peteroid wrote:
int value ;
input >> value ; // ok
enum_var = value ; // error, can't convert?!


int doesn't convert to an enum. You have to cast it:

int value;
input >> value;
enum_var = static_cast<My_Enum>(value);

Tom

Dec 19 '05 #4
Peter Oliphant wrote:
Thanx for the workaraound, which I'm using! : )
It's not a workaround, that's the way of doing it.
However, how you explain this quote form an msdn2 on-line page:

http://msdn2.microsoft.com/en-us/library/17z041d4.aspx

The enum type
ANSI 3.5.2.2 The integer type chosen to represent the values of an
enumeration type
A variable declared as enum is an int.


An enum converts to an int, but an int doesn't convert to an enum
without at least a warning. Once you write your own operator>> for the
enum type, you can simply use that. It would be even better if enum
didn't convert to an int, then you couldn't mix things up accidentally.
I think if you're forced to do a cast, it's a kind of confirmation of
your intentions, telling the compiler that it's not an accident but
that's what you really want.

Tom
Dec 20 '05 #5
OK. But then how do you interpret this statement:
A variable declared as enum is an int.
Is this a falsehood? It certainly doesn't suggest what you've said, and it
is the ENTIRETY of the explanation on MSDN2 for the concept of 'enum
type'...

http://msdn2.microsoft.com/en-us/library/17z041d4.aspx

[==P==]

"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:OY**************@TK2MSFTNGP10.phx.gbl... Peter Oliphant wrote:
Thanx for the workaraound, which I'm using! : )


It's not a workaround, that's the way of doing it.
However, how you explain this quote form an msdn2 on-line page:

http://msdn2.microsoft.com/en-us/library/17z041d4.aspx

The enum type
ANSI 3.5.2.2 The integer type chosen to represent the values of an
enumeration type
A variable declared as enum is an int.


An enum converts to an int, but an int doesn't convert to an enum without
at least a warning. Once you write your own operator>> for the enum type,
you can simply use that. It would be even better if enum didn't convert to
an int, then you couldn't mix things up accidentally. I think if you're
forced to do a cast, it's a kind of confirmation of your intentions,
telling the compiler that it's not an accident but that's what you really
want.

Tom

Dec 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2143
by: Kenneth | last post by:
I want to define an enum for possible languages in my application. My enum look like this: Public Enum Languages English = 0 Portuguese = 1 End Enum How can i reuse this enum in the rest of...
0
1734
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with...
8
8494
by: Bruno BAGUETTE | last post by:
Hello, I have to migrate a MySQL database to a PostgreSQL database without procedures. The problem is that this MySQL database uses ENUM, do you see what can I do to migrate ENUM into...
6
4646
by: randy1200 | last post by:
The following enum is given to me, and I can't change it: enum yo { ONE, TWO, THREE }; I have the following: char test = "ONE"; Any ideas on how to see if the string in "test" is in the...
1
2599
by: Fei Li | last post by:
Hi, Whi can help to expain what logic is when I cast enum a to b? Thanks
2
3390
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
2
1834
by: Alex Feldman | last post by:
Which of the following is better? Defining an enum type inside a class as a nested type, or in the the namespace? An example of nested type enumerated type would be: public Class Product...
1
2539
by: PSN | last post by:
can any one tell me if there is a way to redeclare a typedef enum to add new elements at the end .. Ex: "test.h" typedef enum test1 { hcone = 0, hctwo hcthree, hcfour,
12
6676
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: ...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.