Overriding static variables when subclassing

Use a virtual method to get a reference to the static variable.

class Base {
private:
    static A *a;
public:
    A* GetA() {
        return a;
    }
};

class Derived: public Base {
private:
    static B *b;
public:
    A* GetA() {
        return b;
    }
};

Notice that B derives from A here. Then:

void Derived::paint() {
    this->GetA() ...
}

Leave a Comment