title: Construct Derived Class in Base Class' Constructor in C++
body: <p>Suppose I have a class derived from a base class.  Polymorphism allows me to call the derived class' functions through a pointer to the base class (using virtual functions).  In other words, I can have a member of the derived class "pretending" to be a member of the base class.  Keeping that in mind, is it possible for the base class, in its constructor, to actually construct a member of the derived class (which then "pretends" to be a member of the base class, so nothing breaks)?  Logically , I see no reason that this should not work, but I cannot figure out how to do it syntactically.  If this is not possible, why not?</p>
  
  <p>I know that I can have a separate function that constructs a member of the derived class and returns it as a member of the base class.  That is what I will do if this ends up being impossible, but it would be cleaner to have it as a constructor (because that is what this separate function basically is).</p>
  
  <p>EDIT: Here is an example of what I am looking for:</p>
  
  <pre><code>class base
  {
    base()
    {
      this=new derived(); //This is what I am looking for
    }
  
    virtual func();
  };
  
  class derived : public base
  {
    derived() : base()
      {}
  
    func()
    {
      ...
    }
  };
  </code></pre>
  
  <p>As mentioned above, this could be achieved with the following:</p>
  
  <pre><code>base *fake_base_constructor()
  {
    return new derived();
  }
  </code></pre>
  
  <p>In practice, there would be multiple derived classes, and the base class constructor would choose between them based on parameters, but conceptually, you only need one.  </p>