Search code examples
c++enumsdeclarationenumerator

Is the enum inside a class static?


In the bellow, the values of enum inside the class can be accessed by the name of the class.(I didn't even instantiate the class!)

class Shifting
{
public:
  enum Value: char
  {
    UP, RIGHT, DOWN, LEFT
  };

private:
  Value value_;
};


std::cout << Shifting::RIGHT << std::endl; // 1

Does this mean that enum within a class is static?

If not, how to statically declare an enum?


Solution

  • This

    enum Value: char
    {
      UP, RIGHT, DOWN, LEFT
    };
    

    this is a declaration of a type. It is not a data member of the enclosing class. The class has only this private data member.

    Value value_;
    

    of the enumeration data.

    An enumerations declaration declares named enumerators. But they in turn are not data members of the enclosing class.

    It is the same if you will declare a nested structure inside a class. For example

    struct A
    {
        struct B
        {
            int x = 10;
        };
    
        B b;
    };
    

    Here is only one data member of the class A that is B b. The data member inside the structure declaration only provides the declaration of the structure B.