Reputation: 97
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 ?
Upvotes: 0
Views: 2329
Reputation: 54589
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.
For example:
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;
};
Upvotes: 2