This is a C++ interview test question not homework.
#include <iostream>
using namespace std;
enum months_t { january, february, march, april, may, june, july, august, september,
october, november, december} y2k;
int main ()
{
cout << "sizeof months_t is " << sizeof(months_t) << endl;
cout << "sizeof y2k is " << sizeof(y2k) << endl;
enum months_t1 { january, february, march, april, may, june, july, august,
september, october, november, december} y2k1;
cout << "sizeof months_t1 is " << sizeof(months_t1) << endl;
cout << "sizeof y2k1 is " << sizeof(y2k1) << endl;
}
Output:
sizeof months_t is 4
sizeof y2k is 4
sizeof months_t1 is 4
sizeof y2k1 is 4
Why is the size of all of these 4 bytes? Not 12 x 4 = 48 bytes?
I know union elements occupy the same memory location, but this is an enum.
enum month : char { january, ... };
. Class enums, aka scoped enums, are another variety of classes that create an enclosing namespace for its constants. The thing is that class enums are always of a fixed underlying type (int
if none specified). – Earthiness