Something Pimpl Does Not Do
Pimpl (especially its variation with pointer semantics) might well be classified as yet another deployment of the smart-pointer idiom. However, the similarity with boost::shared_ptr and the like does not go far. Pimpl's primary goal is implementation hiding. For Pimpl the smart-pointer behavior is secondary and somewhat incidental rather than the primary design objective (as for boost::shared_ptr
, std::auto_ptr
, etc.). Due to different design goal, Pimpl possesses far stronger association (and deliberate coupling) between the external interface and internal implementation classes. More so, it does not provide the dereferencing functionality (something expected from purpose-built smart pointers). Whatever there might be in the internal implementation of a Pimpl-based class, it is unreasonable to expect (and incorrect to provide) public access to that implementation via operator->()
. In fact, it is outright impossible for a properly implemented Pimpl-based class. After all, the Pimpl idiom is about implementation hiding and it is not called hiding for nothing.
Pimpl and Dynamic Polymorphism
The application of the Pimpl idiom to polymorphic class hierarchies is well described in the "Bridge" section of [5]. In a nutshell, as Pimpl splits one class into two distinct classes (interface and implementation), the same goes for hierarchies of classes. With the Pimpl idiom applied a class hierarchy is split into two class hierarchies, with one hierarchy for interfaces and the other separate hierarchy for implementations. For example,
struct Widget : public pimpl<Widget>::pointer_semantics { Widget(parameters); virtual ~Widget(); ... }; struct Button : public Widget { Button(parameters); ... }; struct PushButton : public Button { PushButton(parameters); ... };
And the implementation hierarchy might be looking as follows:
typedef pimpl<Widget>::implementation WidgetImpl; template<> struct pimpl<Widget>::implementation { implementation(parameters) {...} virtual ~implementation() {...} ... }; struct ButtonImpl : public WidgetImpl { ButtonImpl(parameters) : WidgetImpl(parameters) {...} ... }; struct PushButtonImpl : public ButtonImpl { PushButtonImpl(parameters) : ButtonImpl(parameters) {...} ... };
So far, building two separate &mdash: interface and implementation — class hierarchies looks nothing out of the ordinary. However, it gets more interesting when we need to establish correct interface-implementation associations. The standard behavior is that Pimpl-based infrastructure automatically creates those associations between a Foo
interface class and a pimpl<Foo>::implementation
implementation class. That works well for the Widget
class in our example above, as an instance of pimpl<Widget>::implementation
is automatically created and associated with an instance of the Widget
interface class. That is what is needed. Therefore, Widget
constructors still look familiar:
Widget::Widget(parameters) : base(parameters) { ... }
For the derived classes, though, the situation gets somewhat more involved as doing something like:
Button::Button(parameters) : Widget(parameters) { ... }
will result in pimpl<Widget>::implementation
internally created and associated with an instance of Button
when we actually needed ButtonImpl
. Given that the automatically created interface-implementation association is not good for run-time polymorphic classes, we have to manage those associations themselves:
Button::Button(parameters) : Widget(null<Widget>()) { reset(new ButtonImpl(parameteres)); ... } PushButton::PushButton(parameters) : Button(null<Button>()) { reset(new PushButtonImpl(parameteres)); ... }
Above, we
- disable the automatic interface-implementation management by initializing the base class with
null<>();
- explicitly create an instance of the correct implementation class with
new;
- and explicitly create an interface-implementation association with
reset()
.