Re: assignment operator using a constructor
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! ]