PImpl

Learn about PImpl (pointer to implementation).

Overview

Suppose that we have a class with the name Weather. Let’s assume that this class is used by many other classes with hundreds of lines of code. Any changes made to the Weather class (even if they are just to one line) will cause recompilation of the Weather class, plus all the classes (that have hundreds of lines of code) that use the Weather class. As we can see, this behavior is undesirable and can waste a lot of time. PImpl is the idea we use to tackle this situation.

PImpl

The C++ programming technique pointer to implementation, or PImpl, is used to remove the implementation details of a class from its object representation by placing them in a separate class.

In our example, we place the implementation code of Weather in a separate class and access this implementation through a pointer from a Weather class. This pointer is generally called an opaque pointer. The classes that use the Weather class don’t need to be recompiled if the implementation changes.

On large projects, this significantly reduces the compilation time. The compiler firewall idiom is another name for this concept.

Get hands-on with 1200+ tech skills courses.