X
xllx.relient.xllx
I have a question about an example presented in the book "INFORMIT C++
Reference Guide" by Danny Kalev:
<code>
string getmagicword()
(
return string("Supercalifragilisticexpialidocious");
);
string magicw=getmagicword();
</code>
"getmagicword() constructs a local string object. Next, this local
object is copy-constructed on the caller's stack, making a temporary
string object that is used as the argument of magicw's assignment
operator/copy-constructor. Next, magicw's copy constructor executes, at
last.
Yes, you heard me right: to initialize magicw, it takes no less than:
One constructor call for the local object inside getmagicword().
Two copy constructor calls: one for copying the local object onto the
caller's stack frame, and then another call for copying that copy to
magicw.
Two destructor calls: one for the object created inside getmagicword
and one for the temp copied onto the caller's stack frame."
Now, my question is: Would the same thing happen (a temporary being
created) if you passed an object to the function:
<code>
void getmagicword(string object)
(
);
string x;
getmagicword(x); // first example
getmagicword(string()); second example
/* In applying the same principal that the book showed for returning
objects, I came up with the following for passing objects to functions:
*/
/* first example calls constructor, then creates temporary object (out
of local object) on the caller's stack which calls the copy
constructor, then it calls the copy constructor again to initialize the
functions argument with the temporary - Yes?*/
/* second example calls constructor, that creates and returns a
temporary object, then calls the copy constructor to create another
temporary object on the caller's stack and initializes it with the
first temporary object, then it calls the copy constructor again to
initialize the functions argument with the last temporary object. -
Yes? */
</code>
If this is different from returning an object in that a temporary is
not created, then why the double standard? How is the passed in object
initialized to the actual argument in the function definition (without
optimizations of course)?
Thanks
Reference Guide" by Danny Kalev:
<code>
string getmagicword()
(
return string("Supercalifragilisticexpialidocious");
);
string magicw=getmagicword();
</code>
"getmagicword() constructs a local string object. Next, this local
object is copy-constructed on the caller's stack, making a temporary
string object that is used as the argument of magicw's assignment
operator/copy-constructor. Next, magicw's copy constructor executes, at
last.
Yes, you heard me right: to initialize magicw, it takes no less than:
One constructor call for the local object inside getmagicword().
Two copy constructor calls: one for copying the local object onto the
caller's stack frame, and then another call for copying that copy to
magicw.
Two destructor calls: one for the object created inside getmagicword
and one for the temp copied onto the caller's stack frame."
Now, my question is: Would the same thing happen (a temporary being
created) if you passed an object to the function:
<code>
void getmagicword(string object)
(
);
string x;
getmagicword(x); // first example
getmagicword(string()); second example
/* In applying the same principal that the book showed for returning
objects, I came up with the following for passing objects to functions:
*/
/* first example calls constructor, then creates temporary object (out
of local object) on the caller's stack which calls the copy
constructor, then it calls the copy constructor again to initialize the
functions argument with the temporary - Yes?*/
/* second example calls constructor, that creates and returns a
temporary object, then calls the copy constructor to create another
temporary object on the caller's stack and initializes it with the
first temporary object, then it calls the copy constructor again to
initialize the functions argument with the last temporary object. -
Yes? */
</code>
If this is different from returning an object in that a temporary is
not created, then why the double standard? How is the passed in object
initialized to the actual argument in the function definition (without
optimizations of course)?
Thanks