EXTERNAL POLYMORPHISM

From the the article External Polymorphism by C. Cleeland and D.C. Schmidt, published in C++ Report. Adds polymorphic functionalities to existing classes.

An abstract interface defines the external polymorphism, ie, the polymorphic methods that are added to the existing classes. This interface contains only a generic (void *) pointer, which will point to the concrete instantiation of an existing class.

Adapter are created for each existing class, through a template parametrized by the existing class. The adapter implements the polymorphic method by calling a global-scope function templated on the existing class, which in turns calls the class specific method. The adapter inherits from the abstract interface. The abstract interface is type-unaware, the adapter adds type-awareness.

A singleton object database can be used to mantain a vector of pointers to the abstract interface, The object database, can invoke the polymorphic method on all the adapters in the vector; the call is dispatched to the proper existing class method.


Sample code