[Exceptional C++ Style] Item 19 - Enforcing Rules for DerivedClasses.

Paul Grenyer paul at paulgrenyer.co.uk
Fri Jan 7 06:02:53 EST 2005


Hi Terje

> This one puzzles me a little:
>
> > class Base {
> > public:
> >   virtual ~Base();
> > private:
> >   Base(const Base&);
> >   Base& operator=(const Base&);
> > };
> >
> > Using the above recommended technique shown in the Base class above at
least
> > ensures that three of the four principle class operations produce
> > compile time errors if they are not defined in the derived class.
>
> Using the above class as a base class will give you a class that can't be
copied
> or assigned to (like deriving from boost::noncopiable), and neither can it
be
> destroyed (!) (you get a link error from missing destructor definition)
This
> last seems to make it rather useless.

I can't put my hand on the book right now, but I guess that the destructor
is intended to be defined. It may be a typo in the summary or something
implied in the book.

> Moreover, there's no point in defining copy constructor, copy assignment
and
> destructor in a derived class, because the lack of definition of these in
the
> base class means that they can't be used. So how can the paragraph above
hold?
> You get errors, anyway (at link time). Am I missing something?

Only if the derived class versions try to call the base class versions.
Imagine an interface class with a private copy-constructor and
copy-assignment-operator and a pure-virtual clone method. The copy
constructor in the concrete type that inherits form this interface can use
its copy contrustructor to implement the clone method, can't it?

Regards
Paul

Paul Grenyer
email: paul at paulgrenyer.co.uk
web: http://www.paulgrenyer.co.uk
articles: http://www.paulgrenyer.dyndns.org/articles/

Darrell "Dimebag" Abbott will be sorely missed.




More information about the Effective-cpp mailing list