Inherit from const class

后端 未结 8 1597
不思量自难忘°
不思量自难忘° 2021-01-12 00:24

I would like to inherit from a class with the const specifier like this:

class Property
{
    int get() const;
    void set(int a);
};

class Co         


        
相关标签:
8条回答
  • 2021-01-12 01:05

    Introduce mutability later in your inheritance tree and derive appropriately:

    class Property
    {
        int get() const;
    };
    class MutableProperty : public Property {
    {
        void set(int a);
    };
    

    And then:

    class ConstChild : public Property { ... };
    class MutableChild : public MutableProperty { ... };
    
    0 讨论(0)
  • 2021-01-12 01:06

    Use a data member or private base class instead of public base class.

    Then you control the access to that member.

    You can make the Property thing an abstract interface if you need polymorphic behavior.

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