Inherit from const class

后端 未结 8 1603
不思量自难忘°
不思量自难忘° 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条回答
  •  -上瘾入骨i
    2021-01-12 01:04

    I had the need for a related problem, which is: to really control/highlight mutable and const access on some class. I did it with this simple reusable template wrapper:

    template 
    class TAccessor : private T
    {
    public:
        const T&    Const() const { return *this; }
        T&          Mutable() { return *this; }
    };
    
    // Example of use:
    template 
    using MyVector = TAccessor>;
    
    
    void main()
    {
        MyVector vector;
    
        vector.Mutable().push_back(10);
    
        int x = vector.Const()[1];
        ...
    }
    

提交回复
热议问题