473,671 Members | 2,420 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 1071
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******** ********@tk2msf tngp13.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******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP10.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
2151
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 my application (in different classes in different projects)?
0
1738
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 consecutive values. The problem is that some functions should only accept some of the ranges. To store values of more than one range/enum in variable one has to use int as storage which imho compromises type safety because any function that wants to...
8
8507
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 PostgreSQL ? Thanks in advance :-)
6
4672
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 enum, and return the enum number if true?
1
2605
by: Fei Li | last post by:
Hi, Whi can help to expain what logic is when I cast enum a to b? Thanks
2
3392
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 and return the name "FirstData"
2
1862
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 Public Enum Status psNormal psCharged End Enum
1
2553
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
6705
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: public enum Days { Sunday };
0
8401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.