In the first example, s1 is already constructed (on the previous line) so the assignment operator will be used to assign the temporary result object to s1.
In the second example, the compiler recognizes that s2 is just being built, so it uses the copy constructor. The compiler is allowed to omit the temporary return object in this case as a performance enhancement -- there's really no need to call the copy constructor twice when the result value can be copied directly into s2.
Hope that helps!
Scott
----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
|