Overriding static variables when subclassing

后端 未结 8 445
南旧
南旧 2020-11-30 11:20

I have a class, lets call it A, and within that class definition I have the following:

static QPainterPath *path;

Which is to say, I\'m dec

8条回答
  •  离开以前
    2020-11-30 11:58

    I haven't tested this, but introducing a virtual function:

    struct Base {
    
        void paint() {
             APath * p = getPath();
             // do something with p
        }
    
        virtual APath * getPath() {
             return myPath;
        }
    
        static APath * myPath;
    };
    
    struct Derived : public Base  {
    
        APath * getPath() {
             return myPath;
        }
        static APath * myPath;
    };
    

    may be what you want. Note you still have to define the two statics somewhere:

    APath * Base::myPath = 0;
    APath * Derived::myPath = 0;
    

提交回复
热议问题