The C++ Pimpl
IntroductionDespite 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. Pimpl in ActionLet'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 }; Our header defines a forward declaration of the Pimpl and other public interface. It stores a private Pimpl_ member pointer and that is all the client will ever see. There will never be any change unless you change the public interface, which you already know you shouldn't. 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 define the private implementation class in the class definition file. We access private methods through the member pimpl_ pointer. 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; Being a PROIf you want to be more of a PRO (professional, that is), here are tips. 1. Private methods calling public methods
2. Using a smart container instead of a raw Pimpl pointer.
3. Defining the private implementation class as a local class instead.
If you need it, get itAt 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. Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|