By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,940 Members | 1,996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,940 IT Pros & Developers. It's quick & easy.

temporary object creation question

P: n/a
Assuming compiler optimizations are set to off, specifically to not
allow the compiler to elide the copy constructor, would the following
hold true?:

If you call a function with an user-defined object by value as it's
first and only argument, and this object was created before the
function call (named object), would the compiler call the copy
constructor to create a temporary object and initialize this object
with the formal argument (what's being passed) and then, another copy
constructor call would be made to initialize the actual argument object
in the function definition with the previous temporary object? In
essense, would two copy constructor calls be made in the situation
presented or would only one copy constructor be called and if so, why?
and.., is returning an object (by value also) any different from
passing?

code:

void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}

Jul 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
xl***************@gmail.com wrote:
Assuming compiler optimizations are set to off, specifically to not
allow the compiler to elide the copy constructor,
Just to let you know, nothing of the kind is defined by the language.
The Standard allows to forgo creating of the temporaries, but says
noting about optimizations' being "set to off".
would the following
hold true?:

If you call a function with an user-defined object by value as it's
first and only argument, and this object was created before the
function call (named object), would the compiler call the copy
constructor to create a temporary object and initialize this object
with the formal argument (what's being passed) and then, another copy
constructor call would be made to initialize the actual argument
object in the function definition with the previous temporary object?
No, why would it create the temporary? The argument (function-local
object) is copy-initialised from the named object.
In essense, would two copy constructor calls be made in the situation
presented or would only one copy constructor be called and if so, why?
No, only one.
and.., is returning an object (by value also) any different from
passing?
Yes, in most cases a temporary has to be created when returning.
code:

void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 8 '06 #2

P: n/a
void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}

Two objects.

The local object "foo" in "main", which is default-constructed.

The local object "foo" in "theCall", which is copy-constructed.
--

Frederick Gotham
Jul 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.