Overriding static variables when subclassing

后端 未结 8 407
南旧
南旧 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? */
    };
    
    0 讨论(0)
  • 2020-11-30 11:38

    You can use virtual functions to achieve your result. This is probably your cleanest solution.

    class A
    {
        protected:
            virtual QPainterPath *path() = 0;
    
        private:
            static QPainterPath *static_path;  /* Lazy initalization? */
    };
    
    QPainterPath *A::path()
    {
        return A::static_path;
    }
    
    class F : public A
    {
        protected:
            virtual QPainterPath *path() = 0;
    
        private:
            static QPainterPath *F_static_path;  /* Lazy initalization? */
    };
    
    QPainterPath *A::path()
    {
        return F::F_static_path;
    }
    
    0 讨论(0)
  • 2020-11-30 11:41

    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() ...
    }
    
    0 讨论(0)
  • 2020-11-30 11:41

    You might be able to do a variant on a mix in or Curiously recurring template pattern

    #include <stdio.h>
    
    typedef const char QPainterPath;
    
    class Base
    {
    public:
        virtual void paint() { printf( "test: %s\n", getPath() ); }
        virtual QPainterPath* getPath() = 0;
    };
    
    template <class TYPE>
    class Holder : public Base
    {
    protected:
        static QPainterPath* path;
        virtual QPainterPath* getPath() { return path; }
    };
    
    class Data1 : public Holder<Data1>
    {
    };
    
    class Data2 : public Holder<Data2>
    {
    };
    
    template <> QPainterPath* Holder<Data1>::path = "Data1";
    template <> QPainterPath* Holder<Data2>::path = "Data2";
    
    int main( int argc, char* argv[] )
    {
    Base* data = new Data1;
    data->paint();
    delete data;
    
    data = new Data2;
    data->paint();
    delete data;
    }
    

    I have just run this code in CodeBlocks and got the following:

    test: Data1
    test: Data2
    
    Process returned 0 (0x0)   execution time : 0.029 s
    Press any key to continue.
    
    0 讨论(0)
  • 2020-11-30 11:43

    If you don't care about the appearance just use A:: or F:: preceding the use of path to choose the correct one, or if you don't like :: name them differently.

    Another option is to use a function to tidy this away, e.g. virtual QPainterPath* GetPath() { return A::path; } in A and QPainterPath* GetPath() { return F::path; } in F.

    Really though this issue is just about how the code looks rather than what it does, and since it doesn't really alter readability I wouldn't fret about this...

    0 讨论(0)
  • 2020-11-30 11:53

    You can't "override" static functions, let alone static member variables.

    What you need is probably a virtual function. These can only be instance functions, so they will not be accessible without class instance.

    0 讨论(0)
提交回复
热议问题