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 8 of 10 Topic 9503 of 9775
Post > Topic >>

Re: copy constructor not being called

by Chris Uzdavinis <cuzdav@[EMAIL PROTECTED] > Apr 16, 2008 at 11:34 AM

On Apr 16, 1:22 am, Marcin Swiderski <sfider.b...@[EMAIL PROTECTED]
> wrote:
> On 15 Kwi, 20:33, suresh <suresh.amritap...@[EMAIL PROTECTED]
> wrote:
>
> >    base b1 = 'x'; //why copy constructor not called here?
>
> This line calls base(const char) constructor and that is the correct
> behaviour. This sytanx means that object b1 is initialized with
> value 'x'. It's equivalent to:
>
> base b1('x');

It's NOT equivalent, it only seems so most of the time.

The difference is that when using assignment syntax, the compiler has
flexibility on how it implements the initialization.  It's allowed
either direct initialization (no tem****ary), or indirect
initialization (create a tem****ary and then copy construct.)

However, when you directly initialize b1 with the argument in
parenthesis, there is no possibility of the extra tem****ary, because
the language doesn't allow for it.

Depending on compiler (or your optimization leve) you may get
different behavior:

base b1('x');  // absolutely no tem****ary
base b1 = 'x'; // may generate tem****ary+copy, may not


This is the reasoning behind the advice of prefering the first form of
initialization (using parenthesis) over the second (assignment
syntax.)

--
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 Tue Jul 8 23:28:24 CDT 2008.