Joe kardin
Joe kardin

Reputation: 3

C++ enum index value

As i understand if we have enum n{sunday //0,monday //1 ,Tuesday //2}

so enum r { one, two = 4, three = 1, four};

that the value of one should be 0 , two is 4 , three is 1 and four is 3

i tried

cout << four

The result is

2

Upvotes: 0

Views: 973

Answers (2)

Tuğberk Kaan Duman
Tuğberk Kaan Duman

Reputation: 960

enum works like this, let's go with your example:

enum r { one, two = 4, three = 1, four};

one starts as 0 since no initial value is defined.

two is defined to be 4 if it wasn't declared as 4, two would be 1 instead.

three is defined to be 1, if it two and three wasn't declared earlier, it'd be 2 instead but it's 1.

four doesn't have any equality, thus it will get the next value assigned, since three was 1, four will be 2.

If no values were assigned, it'd look like this:

enum r { one, two, three, four};

one = 0, two = 1, three = 2, four = 3.

I hope this helps you.

Upvotes: 1

Moia
Moia

Reputation: 2364

Since three is assigned to 1 and four is not, it's assigned as three+1, so four = three + 1

Upvotes: 0

Related Questions