Jump to content

  • Log In with Google      Sign In   
  • Create Account


Enum doesn't occupy any space in an object?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 29 April 2013 - 02:33 PM

So,if I define an enum like:

 

class x{
public:
enum{
hey=0
};

}

 

Is it true that it doesn't occupy any space? And if yes,why?



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7923

Like
3Likes
Like

Posted 29 April 2013 - 02:43 PM

The class doesn't store anything; look at the enum as a type and not a value.



#3 Paradigm Shifter   Crossbones+   -  Reputation: 5235

Like
2Likes
Like

Posted 29 April 2013 - 02:43 PM

It's just a definition. Until you define a member variable it won't occupy any space.

 

class MyBigClass

{

    char aBigArray[1000000];

};

 

uses no space until you instantiate one (by decalring a variable of type MyBigClass).


Edited by Paradigm Shifter, 29 April 2013 - 02:44 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#4 Álvaro   Crossbones+   -  Reputation: 12510

Like
0Likes
Like

Posted 29 April 2013 - 02:44 PM

You are just telling the compiler you are going to use the symbol `x::hey' as meaning 0. That may or may not "occupy any space", depending on what exactly you mean by that. (The size of an object of type x will not be larger because of the enum, the executable will not be larger (except maybe for debugging info), but the source code will use more space, and the compiler will use more memory.)


Edited by Álvaro, 29 April 2013 - 02:45 PM.


#5 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 29 April 2013 - 02:48 PM

Thanks,got it.I was refering to using that code I showed you instead of a const inside a class.

 

So,Inside the class I could write int es[hey];

 

So should I use enums like that in a class instead of a static const if I need to write code like the array above? I understand that the performance I get by doing this is...very small,but just as a concept,is it better?

 

Ofcourse,I could just use int es[10],but when I need the same constant value in many places in a class,defining an enum like the one above,or a static const,might help understand the code faster.


Edited by noatom, 29 April 2013 - 02:50 PM.


#6 Brother Bob   Moderators   -  Reputation: 7923

Like
0Likes
Like

Posted 29 April 2013 - 02:54 PM

Variants of a compile-time constant has no effect on the run-time, because it is compile-time only. So not only is it very small, but exactly zero run-time performance difference. Design-wise... well, that's a different question and depends on what the constant is used for and what it represents.



#7 ApochPiQ   Moderators   -  Reputation: 14673

Like
2Likes
Like

Posted 29 April 2013 - 03:03 PM

Enumerations should be used for groups of related values; if you're using a single-entry enum for a constant, you're going to confuse most people. I suggest sticking with actual constants instead.

#8 Álvaro   Crossbones+   -  Reputation: 12510

Like
0Likes
Like

Posted 29 April 2013 - 03:59 PM

The code should reflect the intent as closely as possible. If you want a constant, use a constant.

Also, if you gave use a piece of code with meaningful identifiers, we would be in a much better position to make good suggestions.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS