我想使用副本分配运算符的默认功能,但可以在操作中执行一些其他任务。因此,基本形式如下所示:
class Test
{
void operator=(Test& that)
{
*this = that; //do the default copy operation
this->foo() //perform some other tasks
}
};
通过创建copy()函数可以轻松完成此操作,但是保留“ =”操作的整洁度可能会很好。
您可以使用基础实现类,并operator=
从子类委托给基础
// Fill this class with your implementation.
class ClsImpl {
// some complicated class
protected:
~ClsImpl() = default;
};
class Cls : public ClsImpl {
public:
Cls& operator=(const Cls& other) {
if (this == &other) { return *this; }
// assign using the base class's operator=
ClsImpl::operator=(other); // default operator= in ClsImpl
this->foo(); // perform some other task
return *this;
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句