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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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