Ben Voigt [C++ MVP] wrote:
However, as a matter of interest, if only the interface is
specified (via pure virtuals in a header) - surely, it means that
EACH child class will have to implement the same functionality
allover again. The idea for exporting classes was for code reuse -
which you seem to be losing, using the Pimpl pattern you describe.
Am I missing something?
Yup. The interface can be implemented in a base class which
provides the implementation, and each derived (or child if you
prefer) class inherits. Only the interface cannot have any
implementation, but it can participate in a full hierarchy.
But if the interface is implemented in a base class, then that base
class needs to be exported, so that its derived classes can correctly
link to the approriate compilation units - so we end up in the same
situation, i.e having to export the (base) class.
Unless I am misunderstanding you, your approach ensures that if we
have a Widget class (say), we will make its interface available as an
ABC, and then every module that needs to use a Widget class implement
the interface. This is not scaleable - as N different classes (in
different library modules) that delegate to the Widget class will
have to write N implementations for the Widget class. The duplicity
of effort that entails (not to mention the increased scope for
errors/nightmare code maintenance etc) is so ridiculous that I have
to assume that I have misunderstood you - please clarify.
The module that you think you want to dllexport a class from...
Instead you declare an interface (class with only pure virtual
functions) in its public header file. Include this from the clients.
Since the members are pure virtual, the header has the complete
definition and no declspec(dllexport) is needed.
Now in that module providing the class, you create a
declspec(dllexport) extern "C" function (or several) for creating
instances, called a factory method. The instances are of a class that
implements the interface. If you want polymorphism between multiple
implementations, multiple classes can provide an implementation by
inheriting the interface. These multiple classes can each implement
the interface independently, or they can inherit from each other to
share an interface.