By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Enumeration in C and C++

P: n/a
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Saikrishna" <ve******@yahoo.com> wrote...
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)
All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;
Here too

t.color = test::RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.


There is no difference between struct and class in this case. Your
'test' _is_ a class. It's just called 'struct'.

Victor
Jul 22 '05 #2

P: n/a
Victor Bazarov replied to Saikrishna:
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)


All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)


[snip]

Or just move the enum outside the struct, if you want to avoid using the
scope resolution operator "::"

enum colorType { RED, GREEN, BLUE };
struct test {
colorType color;
};

David Fisher
Sydney, Australia
Jul 22 '05 #3

P: n/a
"David Fisher" <da***@hsa.com.au> wrote in message
news:H_*******************@nasal.pacific.net.au...
Victor Bazarov replied to Saikrishna:
"Saikrishna" <ve******@yahoo.com> wrote in message
struct test{
enum
{RED, GREEN, BLUE
}color;
};
All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:
enum colorType { RED, GREEN, BLUE };
struct test {
colorType color;
};


A good solution, though when moving the enum to the outside, I normally
rename it. In this case, something like TestColorType. (Although the
colorType seems general enough so we could keep it named that way.)

But note that the original code had an anonymous enum and Victor's solution
respects this, whereas David's design uses a named enum. It's not a problem
though.

Another thing I've done that works sometimes is to make a class that just
defines enums. Then derive A and B from this class of enums. This way
member functions of A and B don't have to use the qualifier.
Jul 22 '05 #4

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:vHcVc.41240$mD.18203@attbi_s02...
"Saikrishna" <ve******@yahoo.com> wrote...
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)


All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;


Here too

t.color = test::RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.


There is no difference between struct and class in this case. Your
'test' _is_ a class. It's just called 'struct'.

Victor


Can someone explain why this would even compile in C? How would it know
where to find 'RED'?
Jul 22 '05 #5

P: n/a
Method Man wrote:
[...]
Can someone explain why this would even compile in C? How would it know
where to find 'RED'?


Questions on C language should generally be asked in comp.lang.c.
My guess is that since C doesn't have a "class scope" concept, the
enumerators are inserted into the surrounding the struct scope (the
global scope in your case). That makes them available to the entire
translation unit.

Check with more knowledgeable C people.

Victor
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.