title: C++ Have One Nested Class Inherit From Another Nested Class
body: <p>I'm writing a cross-platform class hierarchy, and want to keep the platform dependent implementations in their own class (as opposed to having one class with <code>#ifdefs</code>). This is what I have so far, but the compiler is complaining that <code>BaseDef</code> is private. Any help with how I could keep this basic structure while getting it to compile would be greatly appreciated :-)</p>
  
  <p><strong>Edit:</strong> It would seem from <a href="http://cboard.cprogramming.com/cplusplus-programming/85553-friend-cannot-inherit-private-subclass.html" rel="nofollow">here</a> that this isn't possible. Any other way I could keep this general structure and still compile?</p>
  
  <p>Root.h</p>
  
  <pre><code>class Root {
      private:
          class BaseDef {
              virtual void foo() = 0;
              virtual void bar() = 0;
          };
  
          #ifdef _WIN32
          class WinImp;
          #else
          class NixImp;
          #endif
  
          BaseDef* imp;
  
          BaseDef* getImp();
  
      public:
          Root() : imp(getImp()) {}
          void foo();
          void bar();
  };
  </code></pre>
  
  <p>Root.cpp</p>
  
  <pre><code>#include "Root.h"
  void Root::foo() {
      imp-&gt;foo();
  }
  
  void Root::bar() {
      imp-&gt;bar();
  }
  </code></pre>
  
  <p>WinImp.h</p>
  
  <pre><code>#ifdef _WIN32
  #include "Root.h"
  class WinImp : public Root::BaseDef {
      public:
          void foo();
          void bar();
  };
  #endif
  </code></pre>
  
  <p>WinImp.cpp</p>
  
  <pre><code>#include "WinImp.h"
  #ifdef _WIN32
      Root::WinImp::foo() {
  
      }
  
      Root::WinImp::bar() {
  
      }
  
      Root::BaseDef* Root::getImp() {
          return new Root::WinImp();
      }
  #endif
  </code></pre>