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: assignment ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 6 Topic 9519 of 9775
Post > Topic >>

Re: assignment operator using a constructor

by "Jeff Baker" <algorthjb@[EMAIL PROTECTED] > Apr 20, 2008 at 03:48 PM

Using this to start. base& and *this is the correct form.
   base  operator=(const base & a){cout << "assignment operator  " << a.s
<< 
endl; return a.s;}

I am trying to understand how each return type works. I am introducing an 
int return type on next line.
int operator=(const base& a){cout << "asignment operator " << a.s << endl;

return a.s};

What is created if base is change to int? Like base operator =(/...../){} 
copy constructor created. As base& operator=(/.../){}; there is not copy 
constructor
As int return type there no extra constructor and seem equivalent to
 base&  operator=(const base & a)
{
    cout << "assignment operator  " << a.s << endl;
    return *this;
The reference  base& doen't create a copy constructor and there is only a 
temp object for the return.

Which is more efficient the int operator as written or the base type as 
written?
I understand that it might be preferred to use one over the other
determined 
how a problem is handled.

Jeff
>
> Your problem is caused because of the object you are returning.
> You return the character a.s but the return type is a base: So you are
> getting a call to a constructor to make the return object. You are
> also returning the object so you will also get a call to copy
> construct the object out of the function.
>
> What I expect you meant was:
> base&  operator=(const base & a)
> {
>     cout << "assignment operator  " << a.s << endl;
>     return *this;
> }
>



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




 6 Posts in Topic:
assignment operator using a constructor
"Jeff Baker" &l  2008-04-18 15:00:17 
Re: assignment operator using a constructor
arch119 <shambhushrest  2008-04-19 01:45:54 
Re: assignment operator using a constructor
=?UTF-8?B?RXJpayBXaWtzdHL  2008-04-19 02:01:56 
Re: assignment operator using a constructor
Martin York <Martin.Yo  2008-04-19 02:02:15 
Re: assignment operator using a constructor
"Jeff Baker" &l  2008-04-20 15:48:02 
Re: assignment operator using a constructor
Tony Delroy <tony_in_d  2008-04-21 12:15:18 

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:27:39 CDT 2008.