Assigning an int value to enum and vice versa in C++

前端 未结 3 806
灰色年华
灰色年华 2021-01-21 19:09
#include     

typedef enum my_time {
  day,
  night
} my_time;

int main(){    
  // my_time t1 = 1; <-- will not compile
  int  t2 = night;
  return         


        
3条回答
  •  醉话见心
    2021-01-21 19:46

      You could cast to int. This expression makes an explicit conversion of the specified data type (int) and the given value (night).

     int t2 = static_cast(night)
    

提交回复
热议问题