title: How can I get polymorphic behavior in a C++ constructor?
body: <p>I have a base class that I want to look like this:</p>
  
  <pre><code>class B
  {
      // should look like: int I() { return someConst; }
      virtual int I() = 0;
      public B() { something(I()); }
  }
  </code></pre>
  
  <p>The point being to force deriving classes to override <code>I</code> and force it to be called when each object is constructed. This gets used to do some bookkeeping and I need to know what type of object is being constructed (but I otherwise treat the current object as the base class).</p>
  
  <p>This doesn't work because C++ won't let you call an abstract virtual function from the constructor.</p>
  
  <p><em>Is there a way to get the same effect?</em></p>
  
  <hr>
  
  <p>Based on <a href="http://msdn.microsoft.com/en-us/magazine/cc301472.aspx" rel="noreferrer">this link</a> it would seem that the answer is there is no way to get what I want. However what it says is:</p>
  
  <blockquote>
    <p>The short answer is: no. A base class doesn't know anything about what class it's derived from—and it's a good thing, too. <code>[...]</code> That is, the object doesn't officially become an instance of Derived1 until the constructor Derived1::Derived1 begins.</p>
  </blockquote>
  
  <p>However in my case I don't want to know what it <em>is</em> but what it will <em>become</em>. In fact, I don't even care what I get back as long as I the user can (after the fact) map it to a class. So I could even use something like a return pointer and get away with it.</p>
  
  <p>(now back to reading that link)</p>