5

I have a class that looks like this:

namespace R 
{
class R_Class 
{
   enum R_Enum
   {
       R_val1,
       R_val2,
       R_val3
   }
private:
   // some private stuff
public: 
  // some public stuff
}
}

I'm performing unit testing using an automated test tool. The compiler claims that my test harness cannot access the type R::R_Class::R_Enum.

I have no trouble accessing the values within a similar class that is defined as such:

namespace S
{
class S_Class
{
public:
   enum S_Enum
   {
       S_val1,
       S_val2,
       S_val3
   }
}
private:
   // some private stuff
public: 
  // some public stuff
}

Do enums in C++ need to be given explicit visibility directives? If not given any, do they default to private? protected?

Benjamin Borden
  • 380
  • 1
  • 3
  • 8
  • possible duplicate of [Do I need to define an enum as 'public' in its own file so it can be recognized outside its own package?](http://stackoverflow.com/questions/3798045/do-i-need-to-define-an-enum-as-public-in-its-own-file-so-it-can-be-recognized-o) – David Thornley Feb 17 '11 at 15:21
  • @David Thornley That question regards Java and appears to be slightly different. – Mark B Feb 17 '11 at 15:23
  • @Mark B: My mistake on the language, thanks for correcting it. – David Thornley Feb 17 '11 at 15:49

7 Answers7

12

enums obey visibility in classes just like attributes, methods, nested classes or anything else. You need to make it public for outside visibility.

This is so that classes can have private enums used by its own private methods without exposing the enum values to the outside world.

Mark B
  • 95,107
  • 10
  • 109
  • 188
4

All class members, enum or otherwise, are private if you don't specify otherwise. Similarly, all struct members are public if you don't specify otherwise.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

If an enum needs to be public, should you nest it inside another class? I found it convenient and self-documenting to put enums in their own namespace but not another class. You treat the namespace sort of like a class name and you ignore the actual enum name. This allows you to give enum members really good names without conflicting or having to use the hungarian naming approach. In your question, for example, if we have a namespace LedState and an enum State with members On and Off, then you just use LedState::On or LedState::Off. With a typedef outside the LedState namespace, like:

 typedef LedState::State LedStateType 

you can ignore the actual name of the enum "State" so that name can be reused without conflict. If you need to pass an enum, you just declare it of type LedStateType. I wrote a more complete example in a recent blog entry.

Tod
  • 8,192
  • 5
  • 52
  • 93
0

An enum defined within class scope follows the access rules that applies to everything else in class scope. You need to place the enum in a public: section in order for the type to be accessible outside the class.

Erik
  • 88,732
  • 13
  • 198
  • 189
0

They don't default any different than any other member. Since private is the default, that applies to enums just as it would to any other member. Not sure why you think the behavior you're seeing has anything to do with the fact that the member is an enum.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • I think this because I am new to C++ and a colleague told me otherwise. It sounded wrong and I didn't press the issue. It looks like I'll need to... – Benjamin Borden Feb 17 '11 at 15:25
0

private is the default accesibility of C++ classes of classtype 'class'. If you omit specifying it, you cannot access it from the outside of your class.

digg
  • 186
  • 3
  • The "struct" and "union" keywords declare classes too, so you need to specify "private is the default accessibility when using the 'class' class-key", or similar. And visibility != accessibility, an important point. – Fred Nurk Feb 17 '11 at 15:27
0

I assumed that all class members are private by default and I was surprised to discover that structs are the exception.

Related question on SO: default visibility of C++ class/struct members

Community
  • 1
  • 1
Kai
  • 9,444
  • 6
  • 46
  • 61