devxlogo

Assigning Integers to an Enum Type

Assigning Integers to an Enum Type

In C, it is valid to assign integers to an enumerated type. For example:

 /*** valid in C but not C++ ***/enum Status {good, bad};void func(){  Status stat = 1;  /* integer assignment */}

In C++, however, enums are strongly typed. You can only assign enumerators of the same enum type to an enum variable. Explicit type casting is required otherwise. For example:

 //C++enum Status {good, bad};void func(){  Status stat = static_cast (1);  // stat = bad}

devx-admin

Share the Post: