consideri queste classi:C++ 11 parametro ottimale passando
#include <iostream>
#include <string>
class A
{
std::string test;
public:
A (std::string t) : test(std::move(t)) {}
A (const A & other) { *this = other; }
A (A && other) { *this = std::move(other); }
A & operator = (const A & other)
{
std::cerr<<"copying A"<<std::endl;
test = other.test;
return *this;
}
A & operator = (A && other)
{
std::cerr<<"move A"<<std::endl;
test = other.test;
return *this;
}
};
class B
{
A a;
public:
B (A && a) : a(std::move(a)) {}
B (A const & a) : a(a) {}
};
Quando si crea una B
, ho sempre un percorso in avanti ottimale per A
, una mossa per rvalues o una copia per lvalue.
È possibile ottenere lo stesso risultato con un costruttore? Non è un grosso problema in questo caso, ma per quanto riguarda i parametri multipli? Avrei bisogno di combinazioni di ogni possibile occorrenza di lvalue e rvalues nella lista dei parametri.
Questo non è limitato ai costruttori, ma si applica anche ai parametri di funzione (ad es. Setter).
Nota: questa domanda è strettamente relativa a class B
; class A
esiste solo per visualizzare come vengono eseguite le chiamate di copia/spostamento.
Si consiglia di leggere questi: http://stackoverflow.com/questions/8472208/under-what-conditions-should-i-be-thinking-about-implementing-a-move-constructor e http: // stackoverflow. it/questions/4782757/rule-of-three-gets-rule-of-five-with-c11 –
@JamesCuster: volevo solo verificare quante volte sono stati chiamati i rispettivi costruttori/operatori. – fscan