tags:

views:

83

answers:

5

Basically, if I don't do all objects get a copy of all the enum values?

ps: as always, references for your answer are always welcome...

+2  A: 

You cannot declare a nested type (such as enum) static. Syntactically.

Armen Tsirunyan
A: 

You can't, it doesn't make sense. Each instance doesn't store the values, they would store a value that happens to come from the enumerated values.

GMan
Does that imply that any constant member will only exist once in memory, and not once for every instance?
ufotds
The enum values are actually integers, and these integers are used in the code where needed. Normally there is no central storage of all the enum values. The enum just defines a mapping between a token and a numerical value and everywhere the token is used in your code, the compiler replaces it with the numerical value.
Patrick
@ufotds: Yes. It's just a type, it has no storage. (An object of that type does.)
GMan
Clarification. The enum values need not exist as such in memory, outside of machine code that's generated for uses of them. With no use they will in practice not be anywhere (after compilation).
Alf P. Steinbach
A const member does exist for each instance. (It's static members that don't). You must initialize a const member in the constructor, but that value can be different for different instances.
JoshD
@Armen: Ah, OK. I've changed mine to direct at the question.
JoshD
A: 

Enums don't hold all possible values. The various possible values only exist in the code during compile time. At run time, each instance of an enum is essentially an integral value.

JoshD
+2  A: 

With the enum keyword you are actually defining a type, not defining storage like a data member.

It's like defining an inner class, like this:

class X
   {
   private:
      class InnerClass
         {
         ...
         };
   };

The InnerClass definition is just a definition. It just defines the type within the context of the outer class X, so InnerClass can only be referred to as X::InnerClass. But it definitely doesn't take any space in the instances of class X.

Regarding the remark on enums: The enum values are actually integers, and these integers are used in the code where needed. Normally there is no central storage of all the enum values. The enum just defines a mapping between a token and a numerical value and everywhere the token is used in your code, the compiler replaces it with the numerical value.

Patrick
thanks, your comment on GMan's answer is actually the answer that I was lokking for. Can you copy it in your answer, then I will make that the accepted answer.
ufotds
A: 

enum {..}s don't really take up any space in your program. Only objects declared as that enumerated type take up any space. Even then, it is unlikely to be more space than fits in a machine register (since they are comptaible with integer types).

The only place there will ever be any storage associated with all the enumerated names and values defined for the type is during compilation. At runtime, all that information is unnessecary (and thus is not there).

T.E.D.