Search code examples
c++staticclass-variables

C++ : Initializing base class constant static variable with different value in derived class?


I have a base class A with a constant static variable a. I need that instances of class B have a different value for the static variable a. How could this be achieved, preferably with static initialization ?

class A {
public:
    static const int a;
};
const int A::a = 1;

class B : public A {
    // ???
    // How to set *a* to a value specific to instances of class B ?
};

Solution

  • You can't. There is one instance of the static variable that is shared by all derived classes.