Re: operator=
On Jun 6, 6:35 pm, Frank Birbacher <bloodymir.c...@gmx.net> wrote:
James Kanze schrieb:
AFAIK, in both of the above, Base::op=(const Base&) will be
called, but what if 'b' is a reference to a Derived object?
As far as I can see, "d = b" is illegal. There is no operator=
in Derived which can be called with a Base.
Only because the derived operator = hides the one of the base
class. This is a good thing because it makes "d = b" fail.
Yes. I hadn't actually thought about this aspect: hiding seems
natural for me for other reasons. But you're right that it
helps avoid this problem as well.
Of course, the derived class could provide an assignment
operator which worked with a Base const&. There are even a
(very) few special cases where this makes sense, but it's not
something you'd generally want to do.
In general, assignment and polymorphism don't work well
together, and in general, assignment should be banned in
polymorphic classes. But the original poster didn't say why
he was deriving; if the motive isn't polymorphism, then it's
possible that providing assignment in both would be
justified.
In which case the op = could safely be protected and be called
from derived classes.
For example. Typically, however, the base class will have made
it private, without an implementation, in order to avoid it
accidentally being used, so you're sort of stuck. (Of course,
most base classes won't have any data anyway, at least when
inheritance is being used for polymorphism, so you don't need to
call it's assignment operator in the derived class.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34