Question about passing variables to a function

J

Jeroen

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
 
M

mimi

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;
}
 
G

Gavin Deane

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
 
J

Jeroen

Gavin Deane schreef:
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
 
A

anon

Jeroen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top