Robert Larson

  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Private Implementation (PImpl) Design Pattern using C++

Private Implementation (PImpl) Design Pattern using C++

January 12, 2017 by Robert Larson

The Private Implementation Design Pattern (PImpl) is a nice way to hide the internals of a class from other classes that would depend on it otherwise. The advantage of doing so is that it simplifies the header dependency tree which will result in faster build times. For larger systems and/or slow build servers, this may be very desirable. Let’s take a look at the pattern as implemented in C++. We will use our favorite object name Foo to be the class other classes would depend on. We want to limit those dependencies so we provide a private implementation of Foo called FooImpl, which Foo contains as a member. The attributes and behavior are encapsulated to the FooImpl class. You will see there are two data structures members in FooImpl. Without using the Private Implementation pattern, classes using Foo would have been dependent on the two header files for those data structures. Hope this helps anyone interested in the topic. Feedback is always appreciated! Code on!

In Foo.h

// Forward declare your private implementation class
// instead of including a header as that would defeat
// the point of the private implementation pattern
// of limiting header dependencies.

class FooImpl;

class Foo
{
  public:
    Foo();
    ~Foo();

    void doSomething();

  private:
    FooImpl * m_pImpl;
}

In Foo.cc

#include "Foo.h"
#include "FooImpl.h"

Foo::Foo()
  : m_pImpl(new FooImpl())
{
}

Foo::~Foo()
{

}

// delegate doing something to 
// the private implementation object.
Foo::doSomething()
{
   return m_pImpl->doSomething(); 
}

In FooImpl.h

#include "SomeDataStructure.h"
#include "SomeOtherDataStructure.h"

class FooImpl
{
   public:
      FooImpl();
      ~FooImpl();

      void doSomething();

   private:

      SomeDataStructure m_someDataStructure;
      SomeOtherDataStructure m_someOtherDataStructure;
};

In FooImpl.cc

#include "FooImpl.h"

FooImpl::FooImpl()
{

}

FooImpl::~FooImpl()
{

}

FooImpl::doSomething()
{
    m_someDataStructure.doWork();
    m_someOtherDataStructure.doWork();
}

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

Posted in: c++, Design Patterns Tagged: c++, designpatterns
simpleprogrammer.com blogging email course recommendation →
  • Home
  • About Me
  • Contact Me
  • Product Recommendations
  • Client Portal

Recent Posts

  • March Madness 2019 – Weighted Random Bracket Generator
  • Gifts Ideas for Software Developers 2018-2019
  • March Madness 2018 – Second Chance Weighted Random Bracket Generator
  • March Madness 2018 – Weighted Random Bracket Generator
  • Builder Pattern using C++ (NFL Scheduler Example)

Archives

  • March 2019
  • November 2018
  • March 2018
  • August 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017

Follow me on Twitter

My Tweets

Copyright © 2025 Robert Larson.

Grace WordPress Theme by SiteChurch

%d