| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jeroen" |
| Date: |
30 Mar 2007 03:55:59 AM |
| Object: |
Question about passing variables to a function |
Hi,
Assume a function which returns an object of type 'my_class':
my_class get_class()
{
...
}
Now I have the following question:
void my_function(my_class m)
{
...
}
my_function(get_class());
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...
A similar question is about the following code:
my_class my_function()
{
...
return get_class();
}
Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?
Thanx for any answers,
Jeroen
.
|
|
| User: "anon" |
|
| Title: Re: Question about passing variables to a function |
30 Mar 2007 07:03:17 AM |
|
|
Jeroen wrote:
Assume a function which returns an object of type 'my_class':
my_class get_class()
{
...
}
Now I have the following question:
void my_function(my_class m)
{
...
}
my_function(get_class());
The copy constructor will be called in this case
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...
A similar question is about the following code:
my_class my_function()
{
...
return get_class();
}
The copy-constructor does not have to be called in this case - it
depends on compiler you using
.
|
|
|
|
| User: "Gavin Deane" |
|
| Title: Re: Question about passing variables to a function |
30 Mar 2007 04:57:51 AM |
|
|
On 30 Mar, 09:55, Jeroen <no_m...@thanx.com> wrote:
Hi,
Assume a function which returns an object of type 'my_class':
my_class get_class()
{
...
}
Now I have the following question:
void my_function(my_class m)
{
...
}
my_function(get_class());
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...
A similar question is about the following code:
my_class my_function()
{
...
return get_class();
}
Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?
The compiler is allowed to elide the copy in cases like this, so the
copy constructor might not be called. However, even if the copy is
elided, the copy constructor must still be accessible. If my_class
were not copyable (e.g. copy constructor declared private) then the
code would not compile, regardless of whether the compiler would elide
the copy or not.
Gavin Deane
.
|
|
|
| User: "Jeroen" |
|
| Title: Re: Question about passing variables to a function |
30 Mar 2007 06:14:50 AM |
|
|
Gavin Deane schreef:
On 30 Mar, 09:55, Jeroen <no_m...@thanx.com> wrote:
Hi,
Assume a function which returns an object of type 'my_class':
my_class get_class()
{
...
}
Now I have the following question:
void my_function(my_class m)
{
...
}
my_function(get_class());
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...
A similar question is about the following code:
my_class my_function()
{
...
return get_class();
}
Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?
The compiler is allowed to elide the copy in cases like this, so the
copy constructor might not be called. However, even if the copy is
elided, the copy constructor must still be accessible. If my_class
were not copyable (e.g. copy constructor declared private) then the
code would not compile, regardless of whether the compiler would elide
the copy or not.
Gavin Deane
Hi Gavin,
Thanks for answering. Does your answer "The compiler is allowed to elide
the copy in cases like this" refer to both cases I described? So, also
to the first case "my_function(get_class());"?
Thanks,
Jeroen
.
|
|
|
|
|
| User: "mimi" |
|
| Title: Re: Question about passing variables to a function |
30 Mar 2007 04:55:16 AM |
|
|
On 3=D4=C230=C8=D5, =CF=C2=CE=E74=CA=B155=B7=D6, Jeroen <no_m...@thanx.com>=
wrote:
Hi,
Assume a function which returns an object of type 'my_class':
my_class get_class()
{
...
}
Now I have the following question:
void my_function(my_class m)
{
...
}
my_function(get_class());
Question: in this call to my_function, is the copy constructor of
my_class called to make a copy of the object returned by get_class? Or
can a smart compiler skip that step? At first glance I would think that
the copy contructor must be called because a copy of my_class must be
created on the stack when calling my_function...
A similar question is about the following code:
my_class my_function()
{
...
return get_class();
}
Is a copy constructor called to copy the object returned by get_class?
Or can a smart compiler skip that step?
Thanx for any answers,
Jeroen
For primitive types, most modern compilers would skip the step.You
could use the smart to ease your life.But for user-define types,you
should not rely on your compiler's possible optimization.
You could write a simple program to test your compiler's intelligence
quotient.
#include <iostream>
class Foo {
public:
Foo()
{
}
Foo(const Foo &f)
{
std::cout << "Copy constructor for Foo\n";
}
};
Foo test(Foo f)
{
return Foo();
}
int main()
{
test(Foo());
return 0;
}
.
|
|
|
|

|
Related Articles |
|
|