title: Handle Body Idiom in C++
body: <p>I have a class in my library which I want to expose to the users. I don't want to expose the whole class as I might want to make a binary incompatible changes later. I am confused with which of the following ways would be best.</p>
  <h3>Case 1:</h3>
  <pre><code>struct Impl1;
  struct Handle1
  {
    // The definition will not be inline and will be defined in a C file
    // Showing here for simplicity
    void interface()
    {
      static_cast&lt;Impl1*&gt;(this)-&gt;interface();
    }
  }
  
  struct Impl1 : public Handle1
  {
    void interface(){ /* Do ***actual*** work */ }
    private:
    int _data; // And other private data
  };
  </code></pre>
  <h3>Case 2:</h3>
  <pre><code>struct Impl2
  struct Handle2
  {
    // Constructor/destructor to manage impl
    void interface() // Will not be inline as above.
    {
      _impl-&gt;interface();
    }
    private:
    Impl2* _impl;
  }
  
  struct Impl2
  {
    void interface(){ /* Do ***actual*** work */ }
    private:
    int _data; // And other private data
  };
  </code></pre>
  <p>The <em>Handle</em> class is only for exposing functionality. They will be created and managed only inside the library. Inheritance is just for abstracting implementation details. There won't be multiple/different <em>impl</em> classes. In terms of performance, I think both will be identical. Is it? I am thinking of going with the <em>Case 1</em> approach. Are there any issues that needs to be taken care of?</p>