Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


an enumeration based on char rather than an int

Posted on 2006-07-07
Medium Priority
Last Modified: 2010-04-24
Hey, I read that it is possible to base an enumeration on other data types rather than an int to save memory.  each member of an int based enumeration takes 32 bit and according to my book if you base it on a char, everything will fit into one byte.

__value enum WeekDay : char
     Monday = 1, Tuesday..... , Sunday

can anyone explains me what exactly happens here and how does the compiler know to base it on an int? the notation seems like the notation for deriving a class from a base class

Question by:yattias
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1

Author Comment

ID: 17062081
oh and in a regular enumeration, each member takes four bytes right? so does that mean that in the one i listed above, each member would take 1?
LVL 15

Accepted Solution

lakshman_ce earned 536 total points
ID: 17063443
A __value enum is similar to an ordinary C++ enumerated type except for the following:

A __value enum can specify an underlying type.
By default pointer-to rules, a pointer-to a __value enum type is a __gc pointer.

The declaration and behavior of enums has changed from Managed Extensions for C++ to Visual C++ 2005.

The Managed Extensions enum declaration is preceded by the __value keyword. The idea here is to distinguish the native enum from the CLR enum which is derived from System::ValueType, while suggesting an analogous functionality.

Specifying the type is an additional info to call the right enum through functions. Please refer to this link to understand more about all these
LVL 44

Assisted Solution

AndyAinscow earned 532 total points
ID: 17064782
You could encode it bitwise (binary flags).
and int can then store up to 32 possibilities (for the 32 bit version of an int to be precise).

You can test with the binary and (&)   operator for instance.

enum eFirst = 0x01, eSecond = 0x02, eThird = 0x04, eFourth = 0x08....

int iValue = 0x05;  // eFirst + eThird
iValue & eFirst     //TRUE
iValue & eSecond //FALSE
iValue & eThird    //TRUE
iValue & eFourth  //FALSE
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.


Author Comment

ID: 17065393
reply to comment from lakshman_ce

>A __value enum is similar to an ordinary C++ enumerated type except for the following:
>A __value enum can specify an underlying type.
>By default pointer-to rules, a pointer-to a __value enum type is a __gc pointer

when you said underlying type did you mean the type after the colon?

                 __value enum WeekDay : char (in this case the underlying type would be char right?)
and if so, you could specify an underlying type only in __value types and not in regular c++ enumerations?


Author Comment

ID: 17065406
reply to comment from AndyAinscow

>enum eFirst = 0x01, eSecond = 0x02, eThird = 0x04, eFourth = 0x08....

I kinda see what you did here with the hexadecimal but not quite completely, could you explain please? Lets say I want to create an instance of eFirst, how would I access the types (eFirst, eSecond) and so on? what values would they have? thanks
LVL 44

Expert Comment

ID: 17065852
The hex values are the values you use.  The 'result' is made up of various binary values.  There are various binary comparison operators available (and, or, exlusive or...).
What the bitwise comparison does is just pack certain values (eg. closed, paid, not paid....) into ONE variable.  re the first part of you question about saving space.
LVL 15

Expert Comment

ID: 17067485
>>when you said underlying type did you mean the type after the colon?

LVL 49

Assisted Solution

DanRollins earned 532 total points
ID: 17111525
Good "old-fashioned" enum values are always int, but they work a lot like a #define, that is, when the compiler sees it, it uses whatever datatype is in the context.

With that in mind, consider that the actual bit-length of the enum is often irrelevant.  For instance,

      enum WeekDay {  Monday = 1, Tuesday=2, Sunday=7 };

      BYTE DayOfWeek= 1;
      if ( DayOfWeek == Monday ) {

The compiler will use a one-byte comparison.  The generated code will be optimal and storage space for the variable will be minimized.

In trying to understand your question... it occurs to me that you may have an array (or other memory consuming object) and you want to avoid using up four bytes each time your enum is used.  In that case, just define the value as a one-byte value in the array, and let the compiler use its default conversion logic.
      enum WeekDay {  Monday = 1, Tuesday=2, Sunday=7 };
      enum Month {  January= 1, February=2, December=12 };

      struct MyStruct {
            char szName[20];
            WeekDay eDay;     // four bytes
            Month   eMonth;   // four bytes

      struct MyStruct2 {
            char szName[20];
            BYTE eDay;       // one byte
            BYTE eMonth;     // one byte

      MyStruct r;
      r.eDay=  Monday;     // a four-byte data move

      MyStruct2 r2;
      r2.eDay=  Monday;     // a one-byte data move

The second structure stores the data more efficiently.  If you have an array of 1000 of these, the second method will save you 6000 bytes or memory (three bytes for each eDay and three more for each eMonth).  The savings is in memory space... there is often NO advantage in speed since under the covers, the CPU actually has to do *extra* work to isolate a single byte when it is on an odd (non-DWORD) memory address.

-- Dan

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question