Skip to main content
gamedev.net
🔒 Locked

Copy or Assignment

Started by theMadHatter Feb 26, 2009 at 6:41 PM 1 replies 600+ views
Original Post
theMadHatter
theMadHatter
Assume class B.

B DoWork()
{
  B b;
  return b;
}

B b;
b = DoWork();

I ran it myself and it seemed it was copied out of the return and then operator= to assign it to b. Is this correct?
Zahlman
Zahlman
B DoWork(){  B b; // with NRVO, this will be constructed directly in place, ready to return.  return b; // Otherwise, the copy constructor is called here.}{  B b; // An instance is default-constructed  b = DoWork(); // The function is called, and the return value is assigned with the assignment operator.}{  B b = DoWork(); // The local variable is copy-constructed from the result of the function call, except perhaps if the function is inlined and intelligently optimized.}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.