Elpoca
Elpoca

Reputation: 75

Inner class access to outer class nested enum

In the following code fragment, why does 'A' in the inner class CheckForA method not have to be a qualified-id (i.e., return myE == Outer::A)? Is it because both E and Inner are in the same scope? Does class scope work like namespace scope?

class Outer 
{
public:
   enum E{ A, B, C };

   class Inner
   {
   public:
      void Set( E e_ ) { myE = e_; }
      bool CheckForA() const { return myE == A; }
      E myE;
   };

   void Set( E e_ ) { myInner.Set(e_); }
   bool CheckForA() const { return myInner.CheckForA(); };

   Inner myInner;
};


int main()
{
   Outer outer;
   outer.Set(Outer::A);

   return (int)outer.CheckForA();
}

Upvotes: 1

Views: 439

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

According to the C++ Standard

9.7 Nested class declarations

1 A class can be declared within another class. A class declared within another is called a nested class. The name of a nested class is local to its enclosing class. The nested class is in the scope of its enclosing class.

Upvotes: 1

Related Questions