Skip to content

Latest commit

 

History

History
62 lines (39 loc) · 2.77 KB

PIMPL.md

File metadata and controls

62 lines (39 loc) · 2.77 KB

The Pointer to Implementation (PIMPL) is a design pattern used in C++ programming. It's also known as the "Compiler Firewall" or "Cheshire Cat" technique. The core idea behind PIMPL is to hide the implementation details of a class from its interface. Here's a breakdown of how it works and its advantages:

How PIMPL Works

  1. Definition: PIMPL involves defining a class with a pointer to a forward-declared, but not defined, class. This forward-declared class contains the actual implementation details.

  2. Implementation Class: The class's private members and functions are moved into an implementation class, usually defined in the source file (.cpp) rather than in the header file (.h).

  3. Pointer Management: The main class holds a pointer to the implementation class. This pointer is typically a smart pointer (like std::unique_ptr) for automatic memory management.

  4. Forward Declaration: Only a forward declaration of the implementation class is present in the header file, which means the header file does not include details of the implementation.

Advantages of PIMPL

  1. Reduced Compilation Dependency: Changes to the private members of the class don't require recompilation of the classes that use it. This can significantly reduce build times in large projects.

  2. Encapsulation: The internal workings of the class are completely hidden from users, enhancing encapsulation.

  3. ABI Stability: PIMPL can be used to maintain binary compatibility even when the internal implementation changes, which is crucial for library development.

  4. Reduced Memory Footprint: Since the implementation details are not in the header, the memory footprint of including the class in multiple places is reduced.

  5. Easier Platform-specific Implementations: Different implementations of a class (for different platforms, for instance) can be managed more easily without affecting the class's public interface.

Example

// MyClass.h
class MyClassImpl; // Forward declaration

class MyClass {
public:
    MyClass();
    ~MyClass();
    void someFunction();

private:
    std::unique_ptr<MyClassImpl> impl;
};

// MyClass.cpp
class MyClassImpl {
    // Actual implementation details
};

MyClass::MyClass() : impl(new MyClassImpl()) {}
MyClass::~MyClass() {}
void MyClass::someFunction() {
    // Delegate to impl
}

Considerations

  • Runtime Overhead: There is a slight performance cost due to the extra level of indirection.
  • Complexity: It can add complexity to the code, especially for developers not familiar with the pattern.

PIMPL is particularly advantageous in large-scale projects where reducing dependencies and compilation times is critical. It's also useful in library development, where maintaining a stable ABI is important.

code