jeudi 26 juillet 2018

enum type declared inside a class access confusion

In MyClass below, enum MyType is defined inside the class.

In main, I create a variable of MyClass::MyType t. This compiles fine. However, when I wish to assign it a value such as OPEN, there is a compilation error "OPEN was not declared in this scope"

Firstly it probably doesn't make sense declaring an enum type inside the class and limiting its scope there and then creating a variable of that enum type elsewhere, but I'm just trying to understand what's happening.

In the first place, how am I able to create a variable of MyType in main when an object hasn't even been created? Are enums and struct types defined in a class like that implicitly static?

Also, the compiler has access to the enum code, so why doesn't it understand "OPEN"? Thanks

#include <iostream>

using namespace std;

class MyClass
{
    public:

        enum MyType
        {
            OPEN,
            CLOSED
        };

        struct MyStruct
        {
            int val1;
            int val2;
        };
};

int main()
{
    MyClass::MyType t;
    t = OPEN; // compilation error

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire