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

devx-admin

Share the Post: