Accessing enum values from other class

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?

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.


Need Your Help

Viewable area in an HTML email

html css email

Potentially a topic for debate (or not) and I've searched and searched but not found anything. I code quite a lot of HTML emails (for my sins) and am surprised that this debate has only just come u...

Getting Json returns true or false

php jquery ajax json

I have a switch statement which return true or false and and an if and else statement I then use it to make some more validation.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.