On Apr 21, 6:48 am, "Jeff Baker" <algort...@[EMAIL PROTECTED]
> wrote:
> 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;}
>
> int operator=(const base& a){cout << "asignment operator " << a.s <<
endl;
> return a.s};
>
> ...
> base operator =(/...../){}
>
> 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.
While the language doesn't force it, operator= should always return a
reference to the assigned-to object (i.e. return *this;).
This usage is expected: e.g. it will really confuse your users if
if ((my_var = new_value).is_valid()) ...
is not equivalent to
my_var = new_value;
if (my_var.is_valid()) ...
Similarly, one might hack up a class for which:
a < b < c
tests a < b && b < c (rather than the equivalent if "(a < b ? 1 : 0) <
c". I think it's part of the C++ learning process to hack up and try
out a few of these things. But despite the superficial appeal, you'd
find it will typically create subtle "issues" and unmaintainable code
(much faster if you throw in implicit constructors and conversion
operators).
In conclusion, there are just some things you don't mess with, and
assignment operator return types are one of them. Another one's
operator<< return type....
Tony
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|