Enum array initialization

How do I get an array initialized with the enum variables, instead of their integer counterparts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;


enum DAY
{
	Sunday    = 0,
	Monday    = 1,
	Tuesday   = 2,
	Wednesday = 3,
	Thursday  = 4,
	Friday    = 5,
	Saturday  = 6
};



int main()
{
	DAY days[7];

	for (int i = 0; i < 7; ++i)
		days[i] = (DAY)i;

	cout << days[3]; // this outputs 3 instead of Wednesday


	cin.ignore();
	cin.get();
	return 0;
}
The enum tags are just for your convenience, their values are the numbers in the enumeration, and that's what the computer uses.
To illustrate, what you're trying to do is the same as this:
1
2
int money = 10;
cout << money; // this outputs 10 instead of money 

It may not look the same but it is.

The enum hack is an old way of defining integral constants

1
2
3
4
5
6
enum
{
    arr_size = 20
};

int my_arr[arr_size];


arr_size is an int, not a string.

You'll need an array of strings, manually synchronised with your enum, to do what you want. Or use std::map
1
2
3
4
5
6
7
8
std::map<int, std::string> days;
days[0] = "Sunday";
days[1] = "Monday";
...
days[6] = "Saturday";

...
cout << days[1]; // output "Monday" 


OR

1
2
3
4
5
6
7
8
9
std::map<DAY, std::string> days;
days[Sunday] = "Sunday";
days[Monday] = "Monday";
...
days[Saturday] = "Saturday";

...
cout << days[1]; // output "Monday"
cout << days[Saturday]; // output "Saturday" 
Haven't gotten to maps yet, so I guess I'll use string arrays. Kind of disappointing to see this enum doesn't have an intrinsic way of doing what I want. What's the point of using it when you can just use a plain static allocated string array?
What's the point of using it when you can just use a plain static allocated string array?
Enums are useful when you want to represent a bunch of distinct values, like colors Red, Green & Blue, or genders Male and Female. There are plenty of times when you want to represent these as numbers rather than strings.

Although representing the strings with a map will work, notice that the map doesn't change during the lifetime of the program and neither do the strings. As a result, you'll be bringing in a lot of extra code that isn't really necessary.

On my system, your existing code compiles into an executable that looks like this:
$ size foo
   text    data     bss     dec     hex filename
   3877    2072     272    6221    184d foo

This says that the code takes 3877 bytes, initialized data takes 2072 bytes and uninitialized data takes 272 bytes for a total of 6221.

Using a map<Day,std::string>, the size more than doubles:
$ size foo
   text    data     bss     dec     hex filename
  12885    2796     304   15985    3e71 foo

An alternative is a simple array of cstrings:
1
2
3
4
5
6
7
8
9
const char *dayMap[] = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

With this the size goes up by 140 bytes instead of 9,700+:
$ size foo
   text    data     bss     dec     hex filename
   3937    2152     272    6361    18d9 foo


I'm not trying to slam std::map<>, I'm just pointing out that it comes with a cost in space so when the map doesn't change it might not be the right alternative.
Topic archived. No new replies allowed.