enum question

T

Tony Johansson

Hello!!

I have noticed that when you have defined some enum in the class definitions
these are called as if they were class members. Why is it not possible to
access enum BLACK and WHILE by using object t in main.

Ex.

class Test
{ enum Color{BLACK,WHITE}; };

main
{
Test t;
cout << Test::BLACK;
}


//Tony
 
J

John Carson

Tony Johansson said:
Hello!!

I have noticed that when you have defined some enum in the class
definitions these are called as if they were class members. Why is it
not possible to access enum BLACK and WHILE by using object t in main.

Ex.

class Test
{ enum Color{BLACK,WHITE}; };

The sloppy way in which you have written main is something that should be
avoided.
main
{
Test t;
cout << Test::BLACK;
}

Default access in a class is private for enum declarations, just as for
member data and functions. Put it in the public: section.

Incidentally, the declaration of t is not necessary.
 
M

Mike Wahler

Tony Johansson said:
Hello!!

I have noticed that when you have defined some enum in the class
definitions these are called as if they were class members.

That's because they are class members.
Why is it not possible to access enum BLACK and WHILE by using object t in
main.

Ex.

class Test
{ enum Color{BLACK,WHITE}; };

main
{
Test t;
cout << Test::BLACK;
}

Because 'BLACK' is declared 'private' (by default).
Make your enum 'public' and it will work. Alternatively,
leave it as 'private' and only access it from other
class members and/or friends.

This issue doesn't have to do with 'enum' only, it
applies to any class members.

-Mike
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top