Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Making Pimpl Easy


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

  1. disable the automatic interface-implementation management by initializing the base class with null<>();
  2. explicitly create an instance of the correct implementation class with new;
  3. and explicitly create an interface-implementation association with reset().


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.