PImpl Idiom in C++ with Examples
When changes are made to a header file, all sources including it needs to be recompiled. In large projects and libraries, it can cause build time issues due to the fact that even when a small change to the implementation is made everyone has to wait sometime until they compile their code. One way to solve this problem is by using the PImpl Idiom, which hides the implementation in the headers and includes an interface file which compiles instantly.
The PImpl Idiom (Pointer to IMPLementation) is a technique used for separating implementation from the interface. It minimizes header exposure and helps programmers to reduce build dependencies by moving the private data members in a separate class and accessing them through an opaque pointer.
How to implement:
- Create a separate class ( or struct ) for implementation
- Put all private members from the header to that class.
- Define an Implementation class ( Impl ) in the header file.
- In the header file create a forward declaration (a pointer) , pointing at the implementation class.
- Define a destructor and a copy / assignment operators .
The reason to declare explicitly a destructor is that when compiling, the smart pointer ( std::unique_ptr ) checks if in the definition of the type exists a visible destructor and throws a compilation error if it’s only forward declared.
Using a smart pointer is a better approach since the pointer takes control over the life cycle of the PImpl.
Example:
Header file
/* |INTERFACE| User.h file */ #pragma once #include <memory> // PImpl #include <string> using namespace std; class User { public: // Constructor and Destructors ~User(); User(string name); // Asssignment Operator and Copy Constructor User(const User& other); User& operator=(User rhs); // Getter int getSalary(); // Setter void setSalary(int); private: // Internal implementation class class Impl; // Pointer to the internal implementation unique_ptr<Impl> pimpl; }; |
Implementation file
/* |IMPLEMENTATION| User.cpp file */ #include "User.h" #include <iostream> using namespace std; struct User::Impl { Impl(string name) : name(name){}; ~Impl(); void welcomeMessage() { cout << "Welcome, " << name << endl; } string name; int salary = -1; }; // Constructor connected with our Impl structure User::User(string name) : pimpl(new Impl(name)) { pimpl->welcomeMessage(); } // Default Constructor User::~User() = default; // Assignment operator and Copy constructor User::User(const User& other) : pimpl(new Impl(*other.pimpl)) { } User& User::operator=(User rhs) { swap(pimpl, rhs.pimpl); return *this; } // Getter and setter int User::getSalary() { return pimpl->salary; } void User::setSalary(int salary) { pimpl->salary = salary; cout << "Salary set to " << salary << endl; } |
Advantages of PImpl:
- Binary Compatibility: The binary interface is independent of the private fields. Making changes to the implementation would not brake the dependent code.
- Compilation time: Compilation time drops due to the fact that only the implementation file needs to be rebuilt instead of every client recompiling his file.
- Data Hiding: Can easily hide certain internal details such as implementation techniques and other libraries used to implement the public interface.
- Memory Management: Possible increase in memory usage due to more memory allocation than with the default structure which can be critical in embedded software development.
- Maintenance Effort: The maintenance is becoming more complex due to the additional class in order to use pimpl and additional pointer indirection (Interface can be used only via pointer/reference).
- Inheritance: Hidden implementation cannot be inherited, although a class PImpl can.
- Copy-and-Swap Idiom in C++
- SDL library in C/C++ with examples
- ratio_not_equal() in C++ with examples
- set_symmetric_difference in C++ with Examples
- wmemset() in C/C++ with Examples
- Manipulators in C++ with Examples
- ratio_equal() in C++ with examples
- std::mismatch() with examples in C++
- negative_binomial_distribution in C++ with Examples
- cauchy_distribution a() in C++ with Examples
- std::allocator() in C++ with Examples
- C/C++ For loop with Examples
- Pointers in C/C++ with Examples
- memset() in C with examples
- mbrtoc16() in C/C++ with Examples
Disadvantages of PImpl:
Reference: https://en.cppreference.com/w/cpp/language/pimpl
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




