Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing data size of enumerated types C++

Tags:

c++

enums

size

mfc

By default enumerated type variables take the size of integer i.e 4 bytes in memory. Is there any way to convert this to any other data type size.

Am not talking about type casting, but the memory size required to store an enumerated type.I have referred this question But it didn't tell about changing the integer size to any other.Any help.

like image 375
CodeRider Avatar asked Mar 24 '23 20:03

CodeRider


1 Answers

c++11 introduced strongly typed enums (and Strongly Typed Enums (Revision 3)), which permits the specification of the underlying integral type:

#include <iostream>

enum E_ushort : unsigned short { EUS_1, EUS_2 };
enum E_ulong : unsigned long { EUL_1, EUL_2 };

int main()
{
    std::cout << sizeof(E_ushort::EUS_1) << "\n";
    std::cout << sizeof(E_ulong::EUL_1) << "\n";
    return 0;
}

Output:

2
4
like image 89
hmjd Avatar answered Apr 05 '23 20:04

hmjd