For this program
#include <iostream>
using std::cout;
struct C
{
C() { cout << "Default C called!
"; }
C(const C &rhs) { cout << "CC called!
"; }
};
const C f()
{
cout << "Entered f()!
";
return C();
}
int main()
{
C a = f();
C b = a;
return 0;
}
the output I get is:
Entered f()!
Default C called!
CC called!
Since f()
is returning by value, it should return a temporary. As T a = x;
is T a(x);
, wouldn't it call the copy constructor for the construction of a
, with the temporary passed-in as its argument?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…