dimanche 31 juillet 2016

Design pattern for methods that return object and NRVO

I have class:

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);
};

I want to use it as follows:

A finalRes=a.add(b,c).add(a,a);

It works perfectly, there is no any leaks memory. But how to implement a similar behavior and usage, without using NRVO optimization? What standard design patterns exist for this purpose?

Aucun commentaire:

Enregistrer un commentaire