PImpl Idiom in C++ with Examples Last Updated : 27 Dec, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 some time 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 that 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 implementationPut 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: The class definition in the header file included is the public interface of the class.We define a unique pointer instead of a raw one because the object of the interface type is responsible for the lifetime of the object.Since std::unique_ptr is a complete type it requires a user-declared destructor and copy/assignment operators in order for the implementation class to be complete.The pimpl approach is transparent from the user's viewpoint. Changes made to the IMPLementation structure, internally, affect only the file containing it (User.cpp). This means that the user does not need to recompile in order for these changes to get applied. 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); // Assignment 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(move(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(move(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 break 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. Disadvantages of PImpl: 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. Reference: https://en.cppreference.com/w/cpp/language/pimpl Comment More infoAdvertise with us M MihailYonchev Follow Improve Article Tags : Programming Language C++ Practice Tags : CPP Similar Reads C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read Introduction of Object Oriented Programming As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functi 6 min read Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith 9 min read Like