Copy constructor doesn't get called when initialized by functionreturning object of same class

A

abhash

I am bit puzzled at the following piece of code I tried:

----------------------------------------------------------------------------------
#include <iostream>
using namespace std;

class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}
};

Test fun()
{
return Test();
}

int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;

cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}

OUTPUT (when compiled on CC compiler) :

First way of initialization
Cons
Copy cons

Second way of initialization
Cons
 
D

dizzy

abhash said:
I am bit puzzled at the following piece of code I tried:

------------------------------------------------------------
#include <iostream>
using namespace std;

class Test {
public:
Test() { cout<<"Cons\n";}
Test(Test& a) { cout<<"Copy cons\n";}

You probably mean Test(Test const&).
};

Test fun()
{
return Test();
}

int main()
{
cout<<"First way of initialization\n";
Test t1;
Test t2 = t1;

cout<<"\nSecond way of initialization\n";
Test t3 = fun();
return 0;
}

OUTPUT (when compiled on CC compiler) :

First way of initialization
Cons
Copy cons

Second way of initialization
Cons

First of all if you really had your ctor taking a reference to non const as
you showed above then you are using a broken compiler. Your compiler should
have complained of not finding a Test(Test const&) ctor version.
I am intrigued why second initialization does call copy constructor ?
Aren't we passing the temporary object returned by fun() call to the
t3 for copying ?

Because you are one of the lucky ones using a compiler that does RVO. But
still your compiler is broken because while the standard allows compilers
to elude calling the copy ctor in certain situations (such as yours) for
speed benefits, it does require that the object has a proper copy ctor AS
IF it would have been called (even if it's not).

Enable strict standard compliance mode in your compiler (that should get the
compile error Im talking about), then disable any optimization (or enable
debug mode, depends on compiler), then retry the program. You should see
then the copy ctor getting called.
 
D

dizzy

Pete said:
How do you know that it didn't?

Since he said he ran the program and got unexpected output (to run the
program means to compile it, which means what I said).
 
P

Pascal J. Bourguignon

abhash said:
I am intrigued why second initialization does call copy constructor ?
Aren't we passing the temporary object returned by fun() call to the
t3 for copying ?

The compilers are allowed to optimize out this case. So the function
fun can use the storage of t3 directly to construct a Test there,
without any copying.
 
D

dizzy

Pete said:
Huh? How does compiling and running a program and getting unexpected
output establish that the compiler didn't give any diagnostic messages?

Right. I was thinking about compile time error, sorry for not beeing
explicit enough.
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top