title: How to derive from a pimpl class?
body: <p>Let's say the scenario is this.</p>
  
  <ol>
  <li><code>IBase</code> - interface.</li>
  <li><code>BaseImpl</code> - implementation.</li>
  <li><code>BaseStub</code> - allocates <code>std::unique_ptr</code> to <code>BaseImpl</code> named <code>m_impl</code>.</li>
  </ol>
  
  <p>And then how to achieve the following?</p>
  
  <ol>
  <li><code>IDerived</code> - interface.</li>
  <li><code>DerivedImpl</code> - implementation, inherits from <code>BaseImpl</code>.</li>
  <li><code>DerivedStub</code> - allocates <code>std::unique_ptr</code> to <code>DerivedImpl</code> named <code>m_impl</code>, inherits from <code>BaseStub</code>.</li>
  </ol>
  
  <p>It becomes a problem that BaseStub already allocates its implementation, and since <code>DerivedStub</code> does the same, it conflicts.</p>
  
  <pre><code>class IBase
  {
  public:
      virtual void f1() = 0;
  };
  
  class IDerived
  {
  public:
      virtual void f2() = 0;
  };
  
  class BaseImpl : public IBase
  {
  public:
      virtual void f1() override {}
  };
  
  class DerivedImpl : public BaseImpl, public IDerived
  {
  public:
      virtual void f2() override {}
  };
  
  class BaseStub : public IBase
  {
  public:
      BaseStub() { m_impl.reset(new BaseImpl()); }
  
      virtual void f1() override { m_impl-&gt;f1(); }
  
  private:
      std::unique_ptr&lt;BaseImpl&gt; m_impl;
  };
  
  // But this also creates BaseStub::m_impl.
  class DerivedStub : public BaseStub, public IDerived
  {
  public:
      DerivedStub() { m_impl.reset(new DerivedImpl()); }
  
      virtual void f2() override { m_impl-&gt;f2(); }
  
  private:
      std::unique_ptr&lt;DerivedImpl&gt; m_impl;
  };
  </code></pre>