Re: pure operator=
On Jan 13, 2:31 am, Mike -- Email Ignored <m_d_berger_1...@yahoo.com>
wrote:
Pure operator= thus:
class A // abstract
{
virtual A& operator=(const A&); // has a purpose
};
class B : public A // never instantiated alone
{
virtual B& operator=(const B&) = 0; // nothing needed, so pure
So why provide it?
};
class C : public B; // instantiated
{
virtual C& operator=(const C&); // has a purpose
};
Won't compile as long as I leave the assignment operator for
class B pure. Why not? Chapter & verse?
Anytime you declare a function pure virtual, you must declare it
in the derived class. I don't see any B& operator=( B const& )
in C, so C remains abstract.
Note that virtuality and assignment don't work very well
together anyway, and usually, if a class is polymorphic, you
need to inhibit assignment---just declaring the assignment
operator private in the base class is enough. The one
exception, of course, is the letter/envelope idiom, and there,
the only assignment operator is in the base class---you never
assign derived class instances either.
Think of it for a moment. What should happen in the following
case:
Base* p1 = new Derived1 ;
Base* p2 = new Derived2 ;
*p1 = *p2 ;
What should the type of *p1 be after this operation?
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34