title: C++ Inheritance in the Pimpl idiom
body: <p>Let's say I have a <code>class B</code> and a <code>class A : public B</code> which inherits from <code>B</code>. I want to expose the methods of <code>A</code>, which call some methods in <code>B</code>.</p>
  
  <p>Now I want to expose these methods in the pimpl idiom - I'm really not sure how to do this:</p>
  
  <ul>
  <li><p><s>Do both <code>A</code> and <code>B</code> get separate implementation classes <code>B::impl</code> and <code>A::impl : public B::impl</code> such that implementations inherit from each-other? The regular classes then don't inherit: <code>class A</code> and <code>class B</code>?</s></p>
  
  <p>I realized this is not possible, since the implementations are <code>private</code>.</p></li>
  <li><p>The implementations don't subclass <code>B::impl</code> and <code>A::impl</code> but the exposed classes do <code>class B</code> and <code>class A : public B</code>. But then how are the methods in <code>A::impl</code> able to call the methods of the parent in <code>B::impl</code>?</p></li>
  <li><p>Via a pointer in the arguments - see example below.</p></li>
  </ul>
  
  <p>Thanks</p>
  
  <p><strong>Edit:</strong> Here is an example code snippet - is this correct?</p>
  
  <p><strong>test.hpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class B {
  private:
      class impl;
      std::unique_ptr&lt;impl&gt; pimpl;
  
  public:
      B();
      ~B();
      B(B&amp;&amp;) = default; 
      B(const B&amp;) = delete;
      B&amp; operator=(B&amp;&amp;);
      B&amp; operator=(const B&amp;) = delete;
  
      void my_func() const;
  };
  
  class A : public B {
  private:
      class impl;
      std::unique_ptr&lt;impl&gt; pimpl;
  
  public:
  
      A();
      ~A();
      A(A&amp;&amp;) = default; 
      A(const A&amp;) = delete;
      A&amp; operator=(A&amp;&amp;);
      A&amp; operator=(const A&amp;) = delete;
  
      void access_my_func();
  };
  </code></pre>
  
  <p><strong>test.cpp</strong></p>
  
  <pre><code>#include "test.hpp"
  
  // Implementation of B
  class B::impl
  {
  public:
      impl() {};
  
      void impl_my_func() { 
          std::cout &lt;&lt; "impl_my_func" &lt;&lt; std::endl;
          return; 
      };
  };
  
  // Constructor/Destructor of B
  B::B() : pimpl{std::make_unique&lt;impl&gt;()} {};
  B::~B() = default;
  B&amp; B::operator=(B&amp;&amp;) = default;
  
  // Exposed method of B
  void B::my_func() const {
      std::cout &lt;&lt; "B::my_func" &lt;&lt; std::endl;
      pimpl-&gt;impl_my_func();
      return; 
  };
  
  
  // Implementation of A
  class A::impl
  {
  public:
      impl() {};
  
      void impl_access_my_func(const A&amp; a_in) {
          std::cout &lt;&lt; "impl_access_my_func" &lt;&lt; std::endl;
          a_in.my_func();
          return;
      };
  };
  
  // Constructor/Destructor of A
  A::A() : pimpl{std::make_unique&lt;impl&gt;()} {};
  A::~A() = default;
  A&amp; A::operator=(A&amp;&amp;) = default;
  
  // Exposed method of A
  void A::access_my_func() {
      std::cout &lt;&lt; "A::access_my_func" &lt;&lt; std::endl;
      pimpl-&gt;impl_access_my_func(*this);
      return;
  };
  
  
  
  
  // Later in the main.cpp file
  int main() {
  
      // Make an object
      A my_A_object;
      my_A_object.access_my_func();
  
      return 0;
  };
  </code></pre>