Take the 2-minute tour ×

I think this is more a stylistic question, but I have a class with an enum for statistic distribution type (that is only used in this class):

Entity {
  distributionType ds;

I want to make a DistributionType value as a parameter for my constructor:

Entity salesVolume = new Entity(Entity::DistributionType.NORM);

but this doesn't work (I guess it expects DistributionType when it's being passed an int value?)

What is the right way to call a constructor with an enum value that is part of that same class? I could do it by typecasting to int, but this doesn't seem very tight. I could also exclude the enum from the class and define it separately (which I've seen) - is that a more common way to do it?

Thanks guys

share|improve this question
See stackoverflow.com/questions/4639873/… & please remember to up-vote if it helps. –  Jay May 16 '12 at 5:57
Thanks Jay - checked that question, but it was bit beyond my C++ skills! –  Pete855217 May 16 '12 at 6:10
I avoid putting type definitions like this into classes. For me they are things on their own. Binding them to a class leads to code duplication or plain unreadable code if this exact type (logicaly) is needed in another class/place. Lets consider a "day of the week" enum. You probably want to avoid having several day of the week enums living in different classes. If you bind this enum to a class at first use and it gets used as parameter in 500 places and you then realize that you need it also in a second class you have a lot of refactoring to do to bring your codebase back to a logical state. –  user331471 May 16 '12 at 15:03
And it is unflexible because you can't move the enum to another header. Clients will always have to include the full entity header file into their header if the want to use the enum. –  user331471 May 16 '12 at 15:10

3 Answers 3

up vote 1 down vote accepted

With enums, there is no "namespace", so you need this:

Entity* salesVolume = new Entity(Entity::NORM);

C++11 provides "enum classes" or "strongly typed enums" to solve this weirdness. It also allows you to use the enum name as a "scope" for traditional enums, so you could do this too:

Entity* salesVolume = new Entity(Entity::DistributionType::NORM); // C++11 only
share|improve this answer
Thanks Juan and Jay. Without strongly type enums in current C++, sort of defeats the purpose of including the enum in the class! –  Pete855217 May 16 '12 at 6:09
Well, at least it is in the class namespace, and not hanging out in the global one... –  juanchopanza May 16 '12 at 6:12
You could also make it static no? See codeproject.com/Articles/127233/… or stackoverflow.com/questions/3023643/static-struct-in-c –  Jay May 16 '12 at 6:14
@jay that is a possibility, but then you can't use it as a "type" in an function parameter list, and you can still compare statics under different namespaces. The strongly typed enums are safer in this respect. –  juanchopanza May 16 '12 at 6:20
@jay then you can comment under his question and he'll get it. –  juanchopanza May 16 '12 at 13:48

You can refer to it either as Entity::NORM or, more explicitly (in C++11), Entity::DistributionType::Norm.

However, on a stylistic note, this isn't Java. The code you showed will be slower and likely leak memory than just using the natural way of coding in C++, using automatic variables (variables created "on the stack"). Your code also won't compile because new gives you a pointer to the object. However, Instead of



Entity salesVolume(Entity::NORM);
share|improve this answer
Actually, you can't necessarily do this. Maybe your compiler allows it, but it shouldn't be allowed in C++98, C++03. I think C++11 allows it though. –  juanchopanza May 16 '12 at 6:33
I just checked, your first statement is valid in C++11, but not C++03/C++98. –  juanchopanza May 16 '12 at 7:31
Oh, I guess I've been in C++11 land for too long. Editing. –  David Stone May 16 '12 at 14:19

Can you please what error are you getting, since this code should work, Only problems are that :

  1. you have defined ds as distributionType , while it should be DistributionType (capital "D").
  2. Entity salesVolume = new Entity(Entity::DistributionType.NORM); should be replaced with

Entity *salesVolume = new Entity(Entity::Entity.NORM);

Please Note the "*" before sa;esVolume. ie you have to declare it as pointer.

share|improve this answer
Even with the stackoverflow typos corrected, your code doesn't work with gcc ("error: expected primary-expression before '.' token"). I had to use the Answer example, but using the actual variable instance ie. Entity* salesVolume = new Entity(salesVolume->NORM); –  Pete855217 May 16 '12 at 6:23
So the compiler thinks the instance version is different that the latter with an enum type? Interesting... –  Jay May 16 '12 at 13:37

Your Answer


