an enumeration based on char rather than an int

Posted on 2006-07-07
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
  • 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 134 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 133 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

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?

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


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 133 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now