I want to have a static member a
of a base class B
, with the following features:
- It is a vector (likely not the "challenging" part).
- Derived classes
D1
, etc., should be able to have similar static members, with different values. Not all the classes would have them, and if a classD2
does not define it, its correspondinga
would be the lowest in the parent hierarchy. - I want to access any of these members in any of the classes with a single function
getA
defined in the base class. E.g.,D1
should be able to get its owna
(or the lowest in the parent hierarchy, ifD1
does not define it).
So far, the solutions I have seen require redefining getA
in each derived class in the hierarchy (see below).
I want to avoid this for several reasons, e.g.,
I have a large hierarchy, so applying the required changes, and possible future changes (even if unlikely) become cumbersome.
Are there any alternatives to achieve my goal?
Related: