Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: All
Jun 23, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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:

enum Status {good, bad};
void func()
  Status stat = static_cast<Status> (1);  // stat = bad
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date