Enumeration in C and C++

S

Saikrishna

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.
 
V

Victor Bazarov

Saikrishna said:
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
 
D

David Fisher

Victor Bazarov replied to Saikrishna:
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
 
S

Siemel Naran

David Fisher said:
Victor Bazarov replied to Saikrishna:
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.
 
M

Method Man

Victor Bazarov said:
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)


Here too

t.color = test::RED;


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'?
 
V

Victor Bazarov

Method said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top