Graeme
Graeme

Reputation: 4592

typedef an existing enum type, possible?

Given the following:

namespace otherns
{
    enum MyEnum_e { MyEnum_YES, MyEnum_NO };
}

namespace myns
{
    typedef otherns::MyEnum_e MyEnum_e;
}

Why is the following not valid?

int e = myns::MyEnum_YES;

I get a compiler error stating:

'MyEnum_YES' is not a member of 'myns'

Upvotes: 1

Views: 296

Answers (1)

Jan
Jan

Reputation: 1837

Because the enum values live in the namespace otherns, not as a child of MyEnum_e: to reference MyEnum_YES, you type otherns::MyEnum_YES.

You might try this:

namespace otherns
{
    namespace MyEnum_e_space {
    enum MyEnum_e { MyEnum_YES, MyEnum_NO };
    }
    using namespace MyEnum_e_space;
}

namespace myns
{
    using namespace otherns::MyEnum_e_space;
}

Although, using using is discouraged..

Upvotes: 4

Related Questions