C++ Pimpl Idiom | Pointer to Implementation

C++ Pimpl Idiom | Pointer to Implementation

이 글의 핵심

Reduce compile-time dependencies and keep headers slim with the pimpl pattern.

What is pimpl?

Pointer to implementation: public class holds std::unique_ptr<Impl>; Impl is defined only in .cpp.

// widget.h
class Widget {
public:
    Widget();
    ~Widget();
    void doSomething();
private:
    struct Impl;
    std::unique_ptr<Impl> pImpl;
};

Benefits

  • Faster builds — clients don’t include heavy private headers.
  • ABI flexibility — add private members without recompiling all users (within limits).

Destructor / special members

Out-of-line destructor in .cpp where Impl is complete. Implement or = default move operations as needed.

Fast pimpl

Optional small-buffer optimization: inline storage for tiny Impl objects—advanced, measure first.


  • Bridge pattern
  • Pimpl deep dive

Keywords

C++, pimpl, opaque pointer, encapsulation, ABI