- 标识class的成员值不可变(需在constructor函数中初始化)
class Test{public: Test(); ~Test();private: /** \brief 测试成员变量 */ const int m_varTest;};Test::Test() : m_varTest(100){}Test::~Test(){}
- 代替define在class中定义常量(需将成员定义为静态成员,方便限定常量的作用域,使模块具有更强的独立性)
class Test{public: Test(); ~Test(); /** \brief 测试成员变量 */ static const int BUFFER_SIZE;};const int Test::BUFFER_SIZE = 1024;Test::Test(){}Test::~Test(){}int main(){ int dataArr[Test::BUFFER_SIZE]; return 0;}