Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing enum values from other class

Tags:

c++

enums

typedef

In my project, I have an enum defined in a class, that is used throughout that class. During refactoring, that enum was moved to another class. So I simply typedefed it in my original class, like this:

class A {
public:
  enum E {e1, e2};
};
class B {
public:
  typedef A::E E;
};

Now variable definitions, return values, function params, etc. work perfectly. Only when I want to access the values of the enum inside my second class, I still have to qualify them with the surroundig class's name,
e.g. E e = A::e1;

Is there a way to avoid this, or do I have to copy that into every occurance of the enum values?

like image 677
king_nak Avatar asked Nov 04 '22 10:11

king_nak


1 Answers

You put each enumeration into a nested class that you can typedef within your own class:

class A {
public:
  struct E { enum EnumType { e1, e2 } };
};
class B {
public:
  typedef A::E E;
};

Then it's just E::EnumType instead of E but you get full auto-importation.

like image 191
Mark B Avatar answered Nov 09 '22 05:11

Mark B