Another Blog address: http://insaneguy.me
Original link: http://insaneguy.me/2015/04/05/cheshire_cat_in_cpp-pimpl_idiom/
The "Cheshire Cat Technology" in C + + (Cheshire cat Idiom), also known as Pimpl (Pointer to implementation), Opaque Pointer, is a type of interface defined in a class, It is customary to encapsulate private data members in another implementation class. This method is mainly to hide the data of the class and reduce the pressure at compile time.
What the hell is "Cheshire Cat"? is the following goods:
The Cheshire Cat (Cheshire Cat) is a fictional character in Alice's Wonderland (Alice's Adventure in Wonderland), a fairy tale written by the British writer Lewis Carroll Lewis carroll,1832-1898. Image is a grinning cat, has the ability to appear or disappear out of thin air, even after it disappears, its smile still hanging in midair.
– from Baidu Encyclopedia
The ability of the Cheshire Cat is consistent with the Pimpl function, which means that although the data members "disappear" (hidden), our "Cheshire Cat" smile can still play its role.
Here's an example to introduce Pimpl.
1 Data hiding
In C + + we define a class in a header file, such as a simple Student class that is defined in the following way:
//Student.hclassstudent{ Public: Student ();//Constructor~student ();//destructor voidSayHello ();STD::stringGetName ()Const;voidSetName (STD::stringname);intGetage ()Const;voidSetage (intAge);Private:string_name;unsigned int_age;};
Here _name
and _age
is Student
the private data member of the class. However, customers who use this class tend to be more concerned with the interface of the class (which services the class can provide), and we want to hide the Student
private data members of the class, so we can take advantage of the Pimpl pattern: Define an implementation class, Student
encapsulate the class's data into this implementation class, Student
and Class retains a pointer variable to the implementation class. More clarity in code interpretation:
//Student.hclassstudent{ Public: Student ();//Constructor~student ();//destructor voidSayHello ();STD::stringGetName ()Const;voidSetName (STD::stringname);intGetage ()Const;voidSetage (intAge);Private:classCheshirecat;//Forward declarationCheshirecat *_smilecat;};
//Student.cpp#include "student.h"#include <iostream>#include <string>using namespace STD;classstudent::cheshirecat{ Public: Cheshirecat (): _name (string("guy.")), _age ( -) {} ~cheshirecat () {}string_name;int_age;}; Student::student (): _smilecat (NewCheshirecat ()) {}student::~student () {Delete_smilecat;}voidStudent::sayhello () {cout<<"hello! My name is "<< _smilecat->_name <<"."<< Endl;cout<<"I am"<< _smilecat->_age <<"years old."<< Endl;}stringStudent::getname () {return_smilecat->_name;}voidStudent::setname (stringName) {_smilecat->_name = name;}intStudent::getage () {return_smilecat->_age;}voidStudent::setage (intAge) {_smilecat->_age = age;}
OK, now Student
the interface of the class has not changed, but the private data member in the header file disappears, leaving only a smiling Cheshire Cat ( CheshireCat *_smileCat;
).
2 Save compilation Time
Using Pimpl can help us save time on program compilation. Consider the following class:
// A.h#include "BigClass.h"#include "VeryBigClass"class A{//...private: BigClass big; VeryBigClass veryBig;};
We know that there are header files (. h) and implementation files (. cpp) in C + +, and once the header file changes, no matter how small the changes are, all the files referencing it must be recompiled. For a very large project, C + + compilation can take a lot of time, if the code needs to change frequently, it is really not tolerated. Here, if we BigClass big;
VeryBigClass veryBig;
encapsulate and leverage Pimpl into an implementation class, we can reduce the compilation dependency of A.h and reduce the compile time effect:
// A.hclass A {public: // 与原来相同的接口private: struct AImp; AImp *pimpl;};
3 Side effects
Using PIMPL requires allocating and freeing memory on heap space, memory overhead increases, and more indirect pointer jumps are required, so there are some side effects.
Nonetheless, Pimpl is an effective way to implement data hiding and reduce compilation time. Unless significant program performance degradation is caused, it is recommended to use Pimpl for design.
Reference
Http://stackoverflow.com/questions/60570/why-should-the-pimpl-idiom-be-used
Http://en.wikipedia.org/wiki/Opaque_pointer
Pimpl design pattern in technical--c++ of Cheshire Cat