Post constructor initialization
<p>I have a set of objects derived from common base, <code>ApiObject</code>. I need to be able to register all ApiObjects in a separate data structure, but I need to have an actual address of the object being created, not the base class (I'm using multiple inheritance).</p>

<p>I can't put the code to register an object in <code>ApiObject</code> constructor, because it does not know the address of the derived object; nor can I put it in the derived classes' constructors, because we have no way of knowing whether we are actually constructing another derived class (e.g. if class <code>B</code> is inherited from <code>A</code>, and both can be constructed).</p>

<p>So the only option I see is to explicitly call the registration function every time we create an object, as in</p>

<pre><code>B* b = new B(...);
RegisterObject(b);
</code></pre>

<p>However, this doesn't seem to be a very good solution, as I have to remember to call this function every time.</p>

<p>I suppose I should give more context to explain why I'm doing this. The objects are created via an overloaded new operator, and it needs the object to know the context it was created in (Lua state). E.g.</p>

<pre><code>Foo* object = new(L) Foo(...);
// Foo is derived from ApiObject, and we want ApiObject to have a reference to L
</code></pre>

<p>Currently it is done in a somewhat unelegant way - the new operator allocates additional bytes before the object and stores the L pointer in there, along with some additional data to describe the object type. The base class then receives a pointer to this 'metadata' via the init function.<br>
Otherwise, the first thing that comes to mind are virtual functions, but they can't be called from the constructor, so I'd have to register the base <code>ApiObject</code> pointer but only call the virtual function at some later point, and I'm not sure that's prettier than my current implementation.</p>
