Re: operator=
On Jun 6, 4:59 am, "Daniel T." <danie...@earthlink.net> wrote:
"Corey Cooper" <Co...@EntecSystems.net> wrote:
I have a class for which I needed to create an operator=
function. I then use that as a base class for another
class, which also needs an operator=.
That sounds like a very bad idea.
void fn(Base& b, Derived& d)
{
b = d;
d = b;
}
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.
I don't think the base class should have an op= function. I
think the right thing to do is to make the Base::op= private
and not implemented.
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.
--
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