The C++ Pimpl
Describes a method allowing you to modify the private interface of your class while avoiding recompilation of code using the public interface.
Despite the similarity, the C++ Pimpl has nothing to do with sex. The C++ Pimpl is a short form for "Private Implementation" idiom, and its usage is popularized in Herb Sutter's Guru of the Week (www.gotw.ca) column. (Note: The L behind is used to protect the underage)
The Pimpl idiom describes a way for making your header files impervious to change. You often hear advices like "Avoid change your public interface!" So you can modify your private interface, but how can you avoid recompilation when your header file defines the private methods. This is what the Pimpl does - Reduce compilation damages when your private interface changes.
Just like real life, we often judge a Pimpl by what it has to offer, so let's see what it does.
[size="5"]Pimpl in Action
Let's get straight to business. Here is the Pimpl.
Note: I avoid unnecessary things like header inclusion guards for simplicity.
// In MyClass.h
class MyClassImp; // forward declaration of Pimpl
class MyClass
{
public:
MyClass ();
~MyClass();
MyClass( const MyClass &rhs ); // undefined for simplicity
MyClass& operator=( MyClass );
void Public_Method();
private:
MyClassImp *pimpl_; // the Pimpl
};The definition file goes as follows
// In MyClass.cpp
#include "MyClass.h"
class MyClassImp
{
public:
void Private_Method() {} // dummy private function
int private_var_; // a private variable
};
MyClass::MyClass() : pimpl_( new MyClassImp() )
{
}
MyClass::~MyClass()
{
delete pimpl_;
}
void MyClass::Public_Method()
{
pimpl_->Private_Method(); // do some private work
pimpl_->private_var_ = 3;
}We can add, remove, and change the implementation class anyway we like. The client is totally insulated through the forward declaration and pointer declaration of Pimpl;
[size="5"]Being a PRO
If you want to be more of a PRO (professional, that is), here are tips.
1. Private methods calling public methods
Often your private methods need to call public methods. If that is the case, the private implementation class can store a reference to the main class and use the reference to access the main class. Watch out for methods calling each other though, it will lead to infinite recursion.
2. Using a smart container instead of a raw Pimpl pointer.
In the example, I disallowed copying and assignment in the class. Should you want to allow it, you can use a smart pointer class (The STL auto_ptr doesn't cut it) to hold the Pimpl instead. This saves you from having the common shallow/deep copies pointer problems.
3. Defining the private implementation class as a local class instead.
The Pimpl idiom pollutes the namespace with another class. If you want to avoid that, you can implement the private implementation class as a local class instead. However, do note that local classes have many restrictions.
[size="5"]If you need it, get it
At the end of the day, the question you always need to ask is, "Was it worth it?"
The class has to store an additional pointer member. Calling methods require an additional pointer deference call. For small classes (like matrices classes) which are used often, the speed and memory increase may be unacceptable. For other types of classes, the impact is negligible.
Use it appropriately and it should satisfy just right. Remember,
Always practice safe coding. Use an assert.
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Sobeit Void
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion