代码是:
class base{
base(){}
virtual base* copy()const=0;
virtual ~base(){}
};
class derived:public base{
derived(){}
base* copy()const;
~derived(){}
};
base* derived::copy()const{
return new derived(*this);
}
是否需要new
在函数中使用运算符copy()
或代码为什么使用new
运算符?
我是否应该直接返回this
指针,如下所示:
const base* derived::copy()const{
return this;// note: this pointer is const.
}