1、枚举变量
- 作用域内枚举
为了避免不同枚举定义中的枚举量的冲突,C++11采用如下解决方法:
enum class IEnum1 { one, two, three };
enum class IEnum2 { one, one2, two };
也可以使用struct代替上述代码中的class。同时需要使用枚举名来限定枚举量,使用如下:
IEnum1 myNum = IEnum1::two;
- 作用域内枚举的类型转换
常规枚举会自动进行类型转换,但作用域内枚举需要显式的进行类型转换,如下:
enum Old {one, two};
enum class IEnum {two, three};
int i = two;
int j = int(IEnum::two);
2、类内初始化
C++11允许在声明时对成员变量进行初始化,
class Time
{
int m_iHour = 0;
const int m_iNum = 3;
};
这相当于在构造函数中使用初始化列表进行初始化,
Time::Time(): m_iHour(0), m_iNum(3)
{
}