Guarantee use of derived classes as arguments

Any function that accepts a reference or pointer to an object of a given class must be prepared to receive a derived class (if derived classes are allowed) as an actual argument. Some functions have no choice but to accept a reference, for example, the copy constructor. This means that the receiving function must deal with the argument through an interface guaranteed to be preserved in derived classes. This needn't be the public interface; the interface can be public, protected, or even private. However, it must remain a valid interface in all derived classes, or the function call will fail when used with derived classes.

Note that there are two responsibilities here: the base class designer must design an interface for derived classes that allows them to maintain the interface semantics, and clients of a class that permits polymorphism must allow for it.

If you can't make this guarantee, explicitly state in the class' documentation that it can't be used as a public base class.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker