hi folks,

In some places I have favored using an abstract class interface with
exclusively pure virtual functions over the PIMPL pattern. I think it
would merit some discussion of why we should use one over the other.

First, here's a quick example of the difference

https://gist.github.com/wesm/aea2a95000dd12372e0f1a6163dfd2cc

Obviously a lot of details have been omitted.

Here's an example of such a class which may get reworked as such

https://github.com/apache/arrow/blob/apache-arrow-0.14.1/cpp/src/parquet/arrow/reader.h#L289

The abstract/all-virtual base has some benefits:

* No need to implement "forwarding" methods to the private implementation
* Do not have to declare "friend" classes in the header for some cases
where other classes need to access the methods of a private
implementation
* Implementation symbols do not need to be exported in DLLs with an
*_EXPORT macro

There are some drawbacks, or cases where this method cannot be applied, though:

* An implementation of some other abstract interface which needs to
appear in a public header may not be able to use this approach.
* My understanding is that the PIMPL pattern will perform better for
non-virtual functions that are called a lot. It'd be helpful to know
the magnitude of the performance difference
* Complex inheritance patterns may require use of virtual inheritance,
which can create a burden for downstream users (e.g. they may have to
use dynamic_cast to convert between types in the class hierarchy)

I'm far from a C++ expert so I'm always learning as time goes on, but
hopefully other C++ developers have thoughts about this and we can
keep an eye out in code reviews for cases where a simpler
implementation may suffice

- Wes

Reply via email to