Inherit from const class

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

    You can use a template class and a specialization for a constant type:

        template class base_property {
        protected:
          T value;
        };
        template class property : public base_property {
        public:
          const T& get()const { return value; }
          void set(const T& v){ value = v; }
        };
    
        template class property : public base_property {
        public:
          const T& get()const { return value; }
        };
    
        class ConstChild : public property{ };
    

提交回复
热议问题