Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy constructor doesn't get called when initialized by functionreturning object of same class

Reply
Thread Tools

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

 
 
abhash
Guest
Posts: n/a
 
      06-12-2008
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
------------------------------------------------------------------------------------

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 ?
 
Reply With Quote
 
 
 
 
dizzy
Guest
Posts: n/a
 
      06-12-2008
abhash wrote:

> 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.

--
Dizzy

 
Reply With Quote
 
 
 
 
dizzy
Guest
Posts: n/a
 
      06-12-2008
Pete Becker wrote:

> 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).

--
Dizzy

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-12-2008
abhash <> writes:
> 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.

--
__Pascal Bourguignon__
 
Reply With Quote
 
dizzy
Guest
Posts: n/a
 
      06-12-2008
Pete Becker wrote:

> On 2008-06-12 12:35:48 +0200, dizzy <> said:
>
>> Pete Becker wrote:
>>
>>> 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).

>
> 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.

--
Dizzy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
Copy constructor doesn't get called when initialized by functionreturning object of same class abhash C++ 8 06-14-2008 10:53 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
define a copy constructor in a class having data member as an object of another class dalu.gelu@gmail.com C++ 11 11-09-2006 07:49 PM
Re: Why does the copy constructor get called? Tom Widmer C++ 1 11-17-2004 03:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57