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 4 of 6 Topic 9519 of 9831
Post > Topic >>

Re: assignment operator using a constructor

by Martin York <Martin.YorkAmazon@[EMAIL PROTECTED] > Apr 19, 2008 at 02:02 AM

On Apr 18, 2:00 pm, "Jeff Baker" <algort...@[EMAIL PROTECTED]
> wrote:
> How does an assignment operator work? Following the code that I give
below,
> I notice it isn't clear how the base operator works. I am not sure. It
seems
> to me that there is a copy constructor formed that is used after the '='
is
> call. Cout <<
> "assigment operator ", prints first the at the return the copy
constuctor is
> called. Is there a copy constructor created before the print  statement
is
> executed or is it during the return a.s? Or what is going on?
>
> Here is the code:
>
> #ifndef H_H
> #define H_H
>   # include <iostream>
> using namespace std;
> class base
> {
> private:
>     char s;
> public:
>     base( ){}
>     base(const char a ):s(a) {cout << "construction with arg " << int(a)
<<
> endl;}
>     base(const base & a){cout <<"copy constructor " << int(a.s) <<
endl;}
>     base  operator=(const base & a){cout << "assignment operator  " <<
a.s <<
> endl; return a.s;}
>     ~base( ) {cout << "destruction" << endl;}};
>
> #endif
> #include "h.h"
> int main()
> {
>   base b3;
>   base b1 = 'x'; // is equivalent to  base b1('x');
>   base b2 = b1; //copy constructor is called Is seen as Base b2(b1)
>   b3 = b1; // assignment operator called and copy constructor is called
>
>   return 0;
>
> }

{ edits: quoted signature and banner (see the end of this article)
removed, 
please don't quote extraneous material  --  even if the clc++m banner is a
very 
fine one. -mod }


base  operator=(const base & a)
{
     cout << "assignment operator  " << a.s << endl;
     return a.s;
}

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 Sat Jul 26 2:54:51 CDT 2008.