• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ help

Vertimus

Banned
How do i define the data type of an enumeration?

For example, in

enum Foo {
item1,
item2,
item3,
item4,
};

I want to make it "unsigned char" or something

Any ideas?
 
enum is basically a type istelf, well collection but collection of valyes,, by default the values start from zero , so item 1 =0 , item 2 =1, and so on, you can typcase an unsigned char value, to the enum though
 
Originally posted by: kalster
enum is basically a type istelf, well collection but collection of valyes,, by default the values start from zero , so item 1 =0 , item 2 =1, and so on, you can typcase an unsigned char value, to the enum though

Err, what I mean is, if i actrually declare an enum, like

Foo e=item1;

I want to know the size of e.
 
The MSDN site says

enum [tag] [: type] {enum-list} [declarator];

is the declaration for an enum, but I'm not having success with it.

I'm doing

enum foo:unsigned char {item1, item2....};

is there somethign wrong with that?
 
Originally posted by: Vertimus
How do i define the data type of an enumeration?

For example, in

enum Foo {
item1,
item2,
item3,
item4,
};

I want to make it "unsigned char" or something

Any ideas?

You can't change the type of an enum.... enum's are essentially int's, you can do this though:

enum Foo {
item1 = 32,
item2 = 12989,
item3 = 'a',
item4,
};

char c = (char) item3;

If you want to do funky stuff with enum's i.e. have varies properties you can use the enum design pattern, outlined here for java:
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html

You should be able to do something like that in c++....
 
Originally posted by: statik213
Originally posted by: Vertimus
How do i define the data type of an enumeration?

For example, in

enum Foo {
item1,
item2,
item3,
item4,
};

I want to make it "unsigned char" or something

Any ideas?

You can't change the type of an enum.... enum's are essentially int's, you can do this though:

enum Foo {
item1 = 32,
item2 = 12989,
item3 = 'a',
item4,
};

char c = (char) item3;

If you want to do funky stuff with enum's i.e. have varies properties you can use the enum design pattern, outlined here for java:
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html

You should be able to do something like that in c++....

MSDN says differently though...
Text

type is the underlying type of the identifiers. This can be any scalar type, such as signed or unsigned versions of int, short, or long. bool or char is also allowed.

Maybe this works only on microsoft compilers? I can't find anywhere else that says or disproves this.

Edit: added link
 
enums are ints

if you have:

enum foo{item1, item2, item3, item4};

if you do:

cout<<item1<<endl; it will display 0.
 
Originally posted by: Amol
enums are ints

if you have:

enum foo{item1, item2, item3, item4};

if you do:

cout<<item1<<endl; it will display 0.

Can you please find somewhere that says that? I want to make sure, since the msdn site says differently.

Thanks
 
hmmm...

if I don't declare a type in this following code:

enum Foo {
item1,
item2,
item3=0xFFFFFFFF,
item4
};
int main()
{
Foo c=item1;
return 0;
}

I get compile error

test.cc:7: error: overflow in enumeration values at `item4'

I get a entire bunch of syntax errors if I declare the type.

Is the msdn site wrong or something?
 
Originally posted by: Vertimus
Originally posted by: statik213
Originally posted by: Vertimus
How do i define the data type of an enumeration?

For example, in

enum Foo {
item1,
item2,
item3,
item4,
};

I want to make it "unsigned char" or something

Any ideas?

You can't change the type of an enum.... enum's are essentially int's, you can do this though:

enum Foo {
item1 = 32,
item2 = 12989,
item3 = 'a',
item4,
};

char c = (char) item3;

If you want to do funky stuff with enum's i.e. have varies properties you can use the enum design pattern, outlined here for java:
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html

You should be able to do something like that in c++....

MSDN says differently though...
Text

type is the underlying type of the identifiers. This can be any scalar type, such as signed or unsigned versions of int, short, or long. bool or char is also allowed.

Maybe this works only on microsoft compilers? I can't find anywhere else that says or disproves this.

Edit: added link


AFAIK enums are always ints... but the MSDN article is intersting.... according to it, you can do:

enum foo : double{
x1 = 0.3,
x2 = 0.4,
x3 = 0.5
};


never seen this before....

what is it that you are trying to do?

Since the default is int, you can always cast to (char) or (unsigned char).....
 
Originally posted by: Vertimus
Originally posted by: Amol
enums are ints

if you have:

enum foo{item1, item2, item3, item4};

if you do:

cout<<item1<<endl; it will display 0.

Can you please find somewhere that says that? I want to make sure, since the msdn site says differently.

Thanks

My C++ book. I have a test that include enums tomorrow and I was just studying it.

I scanned the pages on enum

enum1
enum2


edit: of course, my C++ book is probably very basic since it's a high school class
 
Originally posted by: statik213
Originally posted by: Vertimus
Originally posted by: statik213
Originally posted by: Vertimus
How do i define the data type of an enumeration?

For example, in

enum Foo {
item1,
item2,
item3,
item4,
};

I want to make it "unsigned char" or something

Any ideas?

You can't change the type of an enum.... enum's are essentially int's, you can do this though:

enum Foo {
item1 = 32,
item2 = 12989,
item3 = 'a',
item4,
};

char c = (char) item3;

If you want to do funky stuff with enum's i.e. have varies properties you can use the enum design pattern, outlined here for java:
http://www.javaworld.com/javaworld/jw-07-1997/jw-07-enumerated.html

You should be able to do something like that in c++....

MSDN says differently though...
Text

type is the underlying type of the identifiers. This can be any scalar type, such as signed or unsigned versions of int, short, or long. bool or char is also allowed.

Maybe this works only on microsoft compilers? I can't find anywhere else that says or disproves this.

Edit: added link


AFAIK enums are always ints... but the MSDN article is intersting.... according to it, you can do:

enum foo : double{
x1 = 0.3,
x2 = 0.4,
x3 = 0.5
};


never seen this before....

what is it that you are trying to do?

Since the default is int, you can always cast to (char) or (unsigned char).....

Actrually, I was looking at the wrong place. The link I gave is for Visual Studio 2005, which has probably weird non-standard C++ rules.

The normal MSDN pages do not give that.

BTW, the page says only bool, char, short, int, and long and their unsigned versions are allowed, so double won't work.

Thanks guys!
 
The intention for typing the enum is to allow comparisons with a specific type of integer.

Notice that only integer types are available, not strings or floating points.

Char is a type of integer, same as boolean.
 
Back
Top