Copy constructor call

Hi !

test is a class,and trol is a member function.

test test::trol(test& a){return a;};

1
2
test a;         //default constructor;
a.trol(a);      //agree,copy constructor called at returning; 


1
2
test a;         //default constructor;
test b=a;       //copy constructor for b's initialization; 


considering this 2 codes,why doesn't the copy constructor get called two times here?

1
2
test a;
test b=a.trol(a);

once for returning a from trol,and second for b's initialization.
The second copy constructor call is removed by the compiler, this is a form of copy elision:
http://en.cppreference.com/w/cpp/language/copy_elision , this case falls under the second bullet point "When a nameless temporary, not bound to any references, would be moved or copied into an object of the same cv-unqualified type". The function trol() returns such a nameless temporary.
Topic archived. No new replies allowed.