", but for classes? Suppose you have a class with an enumeration defined inside it: class MyC...">

C ++: equivalent to "using namespace <namespace>", but for classes?

Suppose you have a class with an enumeration defined inside it:

class MyClass {
  typedef enum _MyEnum{ a } MyEnum;
}

Is there a way to access the value of a from another class without referencing it as MyClass :: a? If the enumeration was contained in a namespace, and not in a class, than "using a namespace"; solves the problem - is there an equivalent way to do this with classes?

+3
source share
5 answers

I'm sure you are stuck in "MyClass :: a". Also ... wouldn't such a defeat be the purpose of placing an enum inside a class in the first place?

+4
source

, , .

class SomeClass {
    typedef MyClass::MyEnum MyEnum;
    MyEnum value;
};

, typedef , SomeClass::MyEnum MyClass::MyEnum.

+3

Enum-:

struct Holder {
    enum MyEnum { a };
};

class User : public Holder {
public:
    // a now visible in class
    MyEnum f() { return a; } 
};

// a visible outside too
User::MyEnum f() { return User::a; }
+1

MyClass ( enum /), no. , , , , MyClass - , .

0

:

class MyClass {
  public:
  // I added a value here intentionally to show you what to do with more than one enum value
  typedef enum _MyEnum{ a, b } MyEnum;
}
using MyClass::a;
using MyClass::b;
//... likewise for other values of the enum
0

Source: https://habr.com/ru/post/1722640/


All Articles