我有课:
class A{
public:
A(int v){
this->v=new int;
*(this->v)=v;
}
~A(){
delete v;
}
A add(A &a, A &b){
A res(0);
*(res.v)=*(a.v)+*(b.v)+*v;
return res;
}
int get(){
return *v;
}
private:
A();
int* v;
void operator=(const A &other);
TreePointer(const A &other);
};
我想按如下方式使用它:
A finalRes=a.add(b,c).add(a,a);
它工作得很好,没有任何内存泄漏。但是如何在不使用 NRVO 优化的情况下实现类似的行为和用法呢?为此目的存在哪些标准设计模式?