Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default enum visibility in C++

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?

like image 926
Benjamin Borden Avatar asked Dec 07 '22 23:12

Benjamin Borden


1 Answers

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.

like image 178
Mark B Avatar answered Dec 24 '22 20:12

Mark B