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:

  1. Create a separate class ( or struct ) for implementation
  2. Put all private members from the header to that class.
  3. Define an Implementation class ( Impl ) in the header file.
  4. In the header file create a forward declaration (a pointer) , pointing at the implementation class.
  5. 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 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    /* |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;
    };

    chevron_right

    
    

    Implementation file

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    /* |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;
    }

    chevron_right

    
    

    Advantages of PImpl: