Why should the “PIMPL” idiom be used? [duplicate]

I think most people refer to this as the Handle Body idiom. See James Coplien’s book Advanced C++ Programming Styles and Idioms. It’s also known as the Cheshire Cat because of Lewis Caroll’s character that fades away until only the grin remains.

The example code should be distributed across two sets of source files. Then only Cat.h is the file that is shipped with the product.

CatImpl.h is included by Cat.cpp and CatImpl.cpp contains the implementation for CatImpl::Purr(). This won’t be visible to the public using your product.

Basically the idea is to hide as much as possible of the implementation from prying eyes.

This is most useful where you have a commercial product that is shipped as a series of libraries that are accessed via an API that the customer’s code is compiled against and linked to.

We did this with the rewrite of IONA’s Orbix 3.3 product in 2000.

As mentioned by others, using his technique completely decouples the implementation from the interface of the object. Then you won’t have to recompile everything that uses Cat if you just want to change the implementation of Purr().

This technique is used in a methodology called design by contract.

Leave a Comment