Inherit from const class

后端 未结 8 1698
不思量自难忘°
不思量自难忘° 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 00:48

    I have a trick, not a clean solution.

    class ConstChild : private Property
    {
        operator const Property () { return *this; }
    };
    

    then

    ConstChild cc;
    cc.set(10); // ERROR
    cc().get();
    

提交回复
热议问题