Sign in to follow this  

simple enum question...

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, simple question about enum's. lets say i do the following: enum { MESG1 = 0, MESG2 = 1 }; this is an anonymous enum then? theres one thing that i dont understand... what is the exact size of an enum? im thinking the compiler determines it based on how many enum's are in the set, but, im not sure, im getting a very strange problem with this in a simpe console chat program using rakknet. basically, i'll do something like this: packet.WriteToPacket(MESG1); packet.WriteToPacket("Hello thar"); packet.Send(); now, on the receving end, the message is never receieved. however, if i do the following: unsigned char msgid = MESG1; packet.WriteToPacket(msgid); packet.WriteToPacket("Hello thar"); packet.Send(); it works! so, why do i have to cast it to an unsigned char? thanks for any help.

Share this post


Link to post
Share on other sites
what is the exact size of an enum?

The size of the integral type that is necessary to represent the enum's largest element. Here, it should be char-sized.

What parameter type does packet.WriteToPacked() expect?

Share this post


Link to post
Share on other sites
it is over-loaded, and can take bool, char, short, long, double, float, int (isnt int and long the same thing???),and a char * w/ len of string.

so, any idea why this is happening? theres about 50 or so enum's in the set, so this should make it a signed char then, right?

thanks for anymore help.

Share this post


Link to post
Share on other sites
isnt int and long the same thing???

They may have the same size on your platform, but they are two different types. Similarly, you do have a char type which may, in practice, be signed or unsigned, but is neither unsigned char nor signed char.

(note that, in C, enums are all int-sized, IIRC)

so, any idea why this is happening?

Yes.

theres about 50 or so enum's in the set, so this should make it a signed char then, right?

No. It may have the same size, but it is not a signed char - it is an (anonymous) enum. On my system, a quick test

#include <stdio.h>

enum { FOO = 0 };

#define DFOO(x) void foo(x) { puts(#x); }

DFOO(bool);
DFOO(char);
DFOO(short);
DFOO(long);
DFOO(double);
DFOO(float);
DFOO(int);
DFOO(char*);

int main()
{
foo(FOO);
}




Shows it to convert to int (and I am to tired to check the Holy Standard tonight, rating++ (if possible) to whomever quotes the appropriate verses).

Share this post


Link to post
Share on other sites
I use bitstreams with RakNet and cast all my enums into ints. Works allright, if your interested in going that route.

You should ask this question here too, if you haven't already. Kevin provides great support for his library.

Share this post


Link to post
Share on other sites

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this