devxlogo

Casts in C++

Casts in C++

Question:
Can I cast an unsigned integer to an enumerator? If so, could you provide an example?

Answer:
You can use static_cast to cast an int to an enumerator. However, it’s your responsibility to ensure that the int is within the valid ranges of the enumeration. Otherwise, the results are undefined.

Here’s an example:

enum Stat {good=0, bad=1};Stat s;s=static_cast  (0); // s=goodenum Stat {good, bad};Stat s;s = static_cast  (0); // s = good

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist