Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: copy constr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 10 Topic 9503 of 9831
Post > Topic >>

Re: copy constructor not being called

by Chris Uzdavinis <cuzdav@[EMAIL PROTECTED] > Apr 15, 2008 at 11:21 PM

On Apr 15, 2:33 pm, suresh <suresh.amritap...@[EMAIL PROTECTED]
> wrote:

> class base
> {
>    char s;
> public:
>    base(const char a ) {cout << "construction with arg" << endl;}
>    base(const base & a){cout <<"copy constructor" << endl;}
>    base & operator=(const base & a){cout << "op=" << endl;}
>    ~base( ) {cout << "destruction" << endl;}
> };

>    base b1 = 'x'; //why copy constructor not called here?

The standard allows for compilers to not create the tem****ary, and
instead directly construct the object from the argument.  Consider it
an optimization.  Note, though, that since it may have different
runtime behavior than if the tem****ary were created, it's not a
transparent optimization.  (Imagine the copy constructor having
visible side effects... with this optimized away they will not occur.
This is allowable behavior, but suggests we should not have
dependencies upon externally visible side effects occurring in copy
constructors!)

That said, the code still must compile AS IF it were creating a
tem****ary and using the copy constructor--even if it doesn't actually
call the copy constructor.  For example, if you made the copy
constructor private, then the code should not compile, regardless if
this optimization exists in your compiler or not.  Clearly, the
correctness of code should not depend on an optimization!

So logically the tem****ary is always created and then copied.  The
rules of C++ require that this be a valid calling sequence, even if
physically, it does not actually happen.

>    base b2 = b1; //copy constructor is called

This is not creating the tem****ary either.  Instead, it is directly
constructing b2 by using the copy constructor.

> the line base b1 = 'x', as per my understanding must create a
> tem****ary object and then it should be copied to b1. But only the
> constructor with argument is called in this example when executed.

Your understanding is not quite right.  "Must" is too strong of a
word.  "May" is better, but since most compilers prefer to generate
good code, "probably doesn't" is a better description.


Hope this helps.

Chris

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 10 Posts in Topic:
copy constructor not being called
suresh <suresh.amritap  2008-04-15 12:33:42 
Re: copy constructor not being called
dimitry.ivanov@[EMAIL PRO  2008-04-15 23:19:01 
Re: copy constructor not being called
d04rp@[EMAIL PROTECTED]   2008-04-15 23:20:45 
Re: copy constructor not being called
David Pol <david@[EMAI  2008-04-15 23:22:50 
Re: copy constructor not being called
Chris Uzdavinis <cuzda  2008-04-15 23:21:49 
Re: copy constructor not being called
Marcin Swiderski <sfid  2008-04-15 23:22:43 
Re: copy constructor not being called
Francis Glassborow <fr  2008-04-16 11:33:30 
Re: copy constructor not being called
Chris Uzdavinis <cuzda  2008-04-16 11:34:41 
Re: copy constructor not being called
Ron Natalie <ron@[EMAI  2008-04-16 15:44:47 
Re: copy constructor not being called
Marcin Swiderski <sfid  2008-04-17 03:47:55 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:54:23 CDT 2008.