C++ help

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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?
 

kalster

Diamond Member
Jul 23, 2002
7,355
6
81
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
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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.
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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?
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
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++....
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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
 

amol

Lifer
Jul 8, 2001
11,680
3
81
enums are ints

if you have:

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

if you do:

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

Vertimus

Banned
Apr 2, 2004
1,441
0
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
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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?
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
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).....
 

amol

Lifer
Jul 8, 2001
11,680
3
81
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
 

Vertimus

Banned
Apr 2, 2004
1,441
0
0
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!
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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.