Overriding static variables when subclassing

后端 未结 8 451
南旧
南旧 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:37

    You probably don't want static variables to the overriden. Maybe you can store a pointer in your class instead?

    class A
    {
        public:
            A() :
                path(static_path)
            {
            }
    
        protected:
            A(QPainterPath *path)
                : path(path)
            {
            }
    
        private:
            QPainterPath *path;
    
            static QPainterPath *static_path;  /* Lazy initalization? */
    };
    
    class F : public A
    {
        public:
            F() :
                A(F_static_path)
            {
            }
    
        private:
            static QPainterPath *F_static_path;  /* Lazy initalization? */
    };
    

提交回复
热议问题