访问另一个类中的static const。

时间:2014-01-08 11:35:09

标签: c++ c++11 static const

在类中声明和定义的静态const变量。如何在同一项目中的另一个类的私有访问中访问它。可能吗?

//in some header file
Class A{
    public:
    //some data

    private:
        static const uint8_t AVar =1;
        //other data
};


//in some another header file
Class B{
    static const Bvar; 
};
//here inside Class B it possible to give Bvar  = AVar ? If yes, How ?

1 个答案:

答案 0 :(得分:2)

避免重复魔术值而不削弱任何一个类的封装的一种简洁方法是将魔术值移动到两个类都可公开访问的其他位置。

例如:

namespace detail {
    enum MAGIC_NUMBER_T {
        MAGIC_NUMBER = 1
    };
}

class A{
  private:
  static const uint8_t AVar = detail::MAGIC_NUMBER;
};

class B{
     static const uint8_t BVar = detail::MAGIC_NUMBER;
};
相关问题