This question already has an answer here:
Why constructor of class that is instantiate by returned value of a function, not be called?
For example, I wrote this code: a temp class with copy and move constructor and writing a string in destructor and I expected when an object created by output of a function, which is created in it's scope, copy constructor must be called but nothing occurs.
template <typename T>
class temp {
public:
T a;
temp() = default;
temp (const temp& t) {
cout<<"copy cons "<<endl;
a = t.a;
}
temp (const temp<int>&& t) noexcept {
cout<<"copy move1 "<<endl;
a = t.a;
}
temp (temp&& t) noexcept {
cout<<"copy move2 "<<endl;
a = t.a;
}
temp(int _a ) : a(_a) {
}
~temp() {
cout<<"~~ temp "<<this<<" "<<a<<endl;
}
};
temp<int> ttt(int b) {
temp<int> bb(b);
cout<<"adrs: "<<&bb<<endl;
return bb;
}
temp<int> tt(int b) {
temp<int> bb = ttt(b);
cout<<"adrs: "<<&bb<<endl;
return bb;
}
temp<int> t = tt(2);
int main ()
{
cout<<"adrs: "<<&t<<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire