lundi 26 février 2018

move semantics with temps allocated with new

I'm just wondering if move semantics are restricted to syntax style B. More specifically, with style B the object is created on the stack and moved. With style A, the object is created on the heap, but it seems can't be moved.

The question very specifically is, can you use move semantics such that the temp is created with NEW? If so, how?

//move c'tor
A(A&& other) : num(other.num), s(other.s){
   other.num = 0;
   other.s = nullptr;     //dyn alloc obj
}

If you do this, it doesn't work (syntax style A).

A a2(new A("blah"));        //error
A a2(move(new A("blah")));  //error

This is ok (syntax style B)

A a2(A("blah"));            //uses c'tor
A a2(move(A("blah")))       //uses move c'tor

Aucun commentaire:

Enregistrer un commentaire