1. 标识class的成员值不可变(需在constructor函数中初始化
    class Test
    {
    public:
        Test();
    
        ~Test();
    
    private:
        /**
          \brief 测试成员变量
         */
        const int m_varTest;
    
    };
    
    Test::Test() : m_varTest(100)
    {
    
    }
    
    Test::~Test()
    {
    
    }

     

  2. 代替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;
    }

     

posted on 2012-12-26 23:30  cqfuture  阅读(137)  评论(0编辑  收藏  举报