title: Can a base class have a member that is an instance of a derived class?
body: <p>Can I have a base class with a member that is an instance of a derived class? How should I forward-declare or include the derived class definition? Or is there another way altogether that I should be doing this?</p>
  
  <pre><code>// base.h
  
  class DerivedClass;  // Is forward declaration sufficient?
  
  class Base {
   public:
    virtual ~Base();
    virtual void DoStuff() = 0;
    void DoSomethingWithD() {
      d_.Foo();
    }
   protected:
    DerivedClass d_;
  };
  </code></pre>
  
  <hr>
  
  <pre><code>// derived.h
  
  #include "base.h"
  
  class Derived : public Base {
   public:
    Derived();
    void DoStuff();
    void Foo();
   private:
    Derived(const Derived&amp;);
    void operator=(const Derived&amp;);
  };
  </code></pre>
  
  <hr>
  
  <pre><code>// other_derived.h
  
  #include "base.h"
  
  class OtherDerived : public Base {
   public:
    OtherDerived();
    void DoStuff();
   private:
    OtherDerived(const OtherDerived&amp;);
    void operator=(const OtherDerived&amp;);
  };
  </code></pre>