mardi 4 avril 2017

Forward declare unscoped enum in class complains private

Why does the following c++11/14 code not work? Here I am forward declaring an enum within the class. Objective is not to have a huge - 100s of values of enums within the class - which makes the class unreadable. I cannot use a separate scoped enum for this - for political reasons.

class A {
public:
  // forward declare the enum
  enum B : int;
  void func (B b) {}
};

// The actual declaration
enum A::B  : int {
    Val1,
    Val2,
};

int main ()
{
    // B is unscoped. so I should be able to use it as below?
    int a = A::Val1;
}

Compilation Error

tmp.cpp: In function ‘int main()’:
tmp.cpp:13:5: error: ‘A::B Val1’ is private
     Val1,
     ^

tmp.cpp:19:16: error: within this context
     int a = A::Val1;
                ^

But the following code works :

int a = A::B::Val1;

Aucun commentaire:

Enregistrer un commentaire