2

I'm learning c++ and doing some exercise from a book right now. I'm asked to write definition of enum which takes 3 values (forward, backward, stop) and array of 15 elements of this type. In this definition I have to set values of first 5 elements of this array. I did it without problems, but it made me think.. If I write:

enum move {FORWARD = 1, BACKWARD, STOP};
move array[15] {FORWARD, BACKWARD, STOP, STOP, STOP};

... first 5 elements will have values of 1, 2, 3, 3, 3 but all after that will have 0. How is it possible since "move" can't take values other than 1, 2 or 3? Why is the rest of array initialized anyway if I specified just first 5 fields? Or maybe it just means those are empty fields?

Please explain in simple way so beginner like me can understand :), thanks.

5
  • 3
    First of don't use move as a name for your enum. Commented Jun 22, 2014 at 21:24
  • There is no such thing as empty field or empty memory area in C++. Three is initialized and uninitialized. For pointers, there is also NULL (or, equivalently, nullptr). Commented Jun 22, 2014 at 21:26
  • @40two, That's what namespaces are for, right? Trying to avoid every single identifier the standard library uses is a waste of time unless there's an actual risk of it conflicting, which should really only happen in a library where you expose something and a stupid user has using directives of both libraries. Commented Jun 22, 2014 at 21:29
  • @ddur, Enums aren't checked for valid values. It would mean paying for something some people don't use. Commented Jun 22, 2014 at 21:31
  • i get it now. thanks for all answers. Commented Jun 22, 2014 at 22:30

3 Answers 3

4

Every enumeration has an underlying type, and supports a range of values. The underlying type is an integer type big enough to store the whole range of values. All integers in that range are valid values (see below) for an object of the enumeration type, even if there's no enumerator with that value. 0 is in the range of values of any enumeration, hence initializing an object of enumeration type with 0 is fine.

Integer values can be converted to the type of the enumeration if they are in the range of values that this enumeration supports.

If you initialize an aggregate (like an array) with braces {..} and provide less initializers than there are elements in the aggregate, the resulting elements will be value-initialized. For fundamental types (like int) and enumeration types, this means they will be initialized from the integer 0, converted to their respective type.

move array[6] {FORWARD, BACKWARD, STOP, STOP, STOP};
// equivalent to:
move array[6] {FORWARD, BACKWARD, STOP, STOP, STOP, static_cast<move>(0)};
Sign up to request clarification or add additional context in comments.

1 Comment

For a very pedantic and thorough discussion, see stackoverflow.com/q/18195312
1

According to the C++ Standard enumerations with a non-fixed underlaying type have minimum value equal to zero if they were defined such a way that their enumerator with minimum value is not negative.

In your code the enumerator with minimum value is FORWARD. Its value is not negative. So the minimum valid value of the enumeration is 0.

Take into account that according to the same C++ Stsandard

It is possible to define an enumeration that has values not defined by any of its enumerators.

As for the array as it has less initializers than there are elements in the array then all elements that have no an initializer will be zero-initialized. And zero is the valid value for the enumeration.

Comments

0

Your enum move is created explicitly with FORWARD = 1, BACKWARD = 2, STOP = 3.

When you try to initialize your array, the rest of the values are zero because you have to give them an explicit value or else it defaults to zero in order to fill the array.

There cannot be "empty memory" in an array and there cannot be a partially initialized array either.

In other words, enum values are an abstraction for int and therefore must be initialized as integers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.