pure operator=

  • Thread starter Mike -- Email Ignored
  • Start date
M

Mike -- Email Ignored

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
};

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?

Thanks for your help.

Mike.
 
A

Alf P. Steinbach

* Mike -- Email Ignored:
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
};

class C : public B; // instantiated

Extranous semicolon.

{
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?

You haven't defined any of the three different assignment operators you
have declared.

Also, they're all private.

But apart from the extranous semicolon noted above, I can't see why the
code shown above shouldn't compile if you supply a main(); it's just
declarations.

Chapter & verse?

That's not what you need.

What you need is to know that virtual assignment operators are generally
a very bad idea -- for they imply run-time type checking.


Cheers, & hth.,

- Alf
 
O

Ole Nielsby

Mike said:
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
};

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?

To override a virtual, you must use the exact same parameter types.
So, the above defines 3 different virtuals, of which B::eek:perator= is
left abstract.
 
J

James Kanze

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top