[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppPimpl
Page overview:
As you see, everything 'really' happening goes on in the .cpp file. Now imaging that MyClass is a heavy used class. When you change the public part of it, all classes using it, need to be recompiled. The same is true if you change private functions, WHEN NOT using the pimpl idiom. When you do use the pimpl idiom, only the .cpp file is changed and this is the only one that needs to be recompiled!
main.cpp:
UnitPimpl.h:
UnitPimpl.cpp:
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppPimpl
(C++) pimpl
Short for 'pointer to implementation'. Using the pimple idion reduces compilation time. Pimpl judiciously [1].Page overview:
- The principle
- A full example
The principle
The trick of the pimpl idiom is, that in the header file of a class a declaration of the implementation is mady using an opaque pointer ('a pointer to a declared, but undefined class'[2]). That is, all private functions will be in the implementation class:
class MyClass
{
public:
MyClass();
void setX(const double&);
double getX() const;
private:
class Impl; //Forward declaration (as Impl) is undefined
Impl * pImpl; //Opaque pointer
};
//MyClass.cpp
- include "MyClass.h"
void Test::setX(const double& x)
{
pImpl->setX(x);
}
double Test::getX() const
{
return pImpl->getX();
}
As you see, everything 'really' happening goes on in the .cpp file. Now imaging that MyClass is a heavy used class. When you change the public part of it, all classes using it, need to be recompiled. The same is true if you change private functions, WHEN NOT using the pimpl idiom. When you do use the pimpl idiom, only the .cpp file is changed and this is the only one that needs to be recompiled!
A full example
A trivial example, but its a full one:main.cpp:
int main(int argc, char* argv[]) { Test test; test.setX(123.456); std::cout << test.getX() << std::endl; return 0; }
- include <iostream>
- include "UnitPimpl.h"
UnitPimpl.h:
//Header guard (for those now knowing):
- ifndef UnitPimplH
- define UnitPimplH
class Test { private: struct TestImpl; const std::auto_ptr<TestImpl> pImpl; public: Test(); ~Test() {}; void setX(const double& x); double getX() const; };
- include <memory>
- endif
UnitPimpl.cpp:
struct Test::TestImpl { double x; }; Test::Test() : pImpl(new TestImpl) { //How RAII can you get? } void Test::setX(const double& x) { pImpl->x = x; } double Test::getX() const { return pImpl->x; }
- include "UnitPimpl.h"
Code links
- #include
- argc
- argv
- auto_ptr
- char
- class
- cout
- iostream
- int
- main
- new
- private
- public
- return
- std::auto_ptr
- std::cout
- void
References
- 1) Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 43: 'Pimpl judiciously.'
- 2) Herb Sutter. Exceptional C++. ISBN: 0-201-61562-2. Item 27.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
