C++ headers - separation between interface and implementation details

c++, coding-style

Solution

Use the "pimpl" idiom:

// header
class My
{
  class impl;
  std::auto_ptr<impl> _impl;
};

// cpp
#include <3pheader.h>
class My::impl
{
  3pObject _object;
};

Problem

One of classes in my program uses some third-party library. Library object is a private member of my class: ``` // My.h #include <3pheader.h> class My { ... private: 3pObject m_object; } ``` The problem with this - any other unit in my program that uses My class should be configured to include 3p headers. Moving to another kind of 3p will jeopardize the whole build... I see two ways to fix this - one is to is to make 3pObject extern and turn m_Object into a pointer, being initialized in constructor; second is to create an "interface" and "factory" classes and export them... Could you suggest another ways to solve that ?

Original source