在另一个类中访问静态const。 [英] Access static const in another class.
本文介绍了在另一个类中访问静态const。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在类中声明和定义的静态const变量。如何在同一项目中另一个类的私有访问中访问它。是否可以?
A static const variable declared and defined in a class. How to access it in the private access of another class in same project. Is it possible?
//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 ?
推荐答案
避免重复魔法值每个类的弱化封装是将魔术值移动到两个类都可公开访问的不同位置。
A clean way to avoid duplication of the magic value without weakening encapsulation of either class is to move the magic value to a different place that is publicly accessible to both classes.
例如:
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;
};
这篇关于在另一个类中访问静态const。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文