initialization of enum array in c++ -
i'm learning c++ , doing exercise book right now. i'm asked write definition of enum takes 3 values (forward, backward, stop) , array of 15 elements of type. in definition have set values of first 5 elements of array. did without problems, made me think.. if write:
enum move {forward = 1, backward, stop}; move array[15] {forward, backward, stop, stop, stop};
... first 5 elements have values of 1, 2, 3, 3, 3 after have 0. how possible since "move" can't take values other 1, 2 or 3? why rest of array initialized anyway if specified first 5 fields? or maybe means empty fields?
please explain in simple way beginner me can understand :), thanks.
every enumeration has underlying type, , supports range of values. underlying type integer type big plenty store whole range of values. integers in range valid values (see below) object of enumeration type, if there's no enumerator value. 0
in range of values of enumeration, hence initializing object of enumeration type 0
fine.
integer values can converted type of enumeration if in range of values enumeration supports.
if initialize aggregate (like array) braces {..}
, provide less initializers there elements in aggregate, resulting elements value-initialized. fundamental types (like int
) , enumeration types, means initialized integer 0
, converted respective type.
move array[6] {forward, backward, stop, stop, stop}; // equivalent to: move array[6] {forward, backward, stop, stop, stop, static_cast<move>(0)};
c++ arrays enums initialization
No comments:
Post a Comment