c++ - Post constructor initialization -
i have set of objects derived common base, apiobject
. need able register apiobjects in separate data structure, need have actual address of object being created, not base class (i'm using multiple inheritance).
i can't put code register object in apiobject
constructor, because not know address of derived object; nor can put in derived classes' constructors, because have no way of knowing whether constructing derived class (e.g. if class b
inherited a
, , both can constructed).
so option see explicitly call registration function every time create object, in
b* b = new b(...); registerobject(b);
however, doesn't seem solution, have remember call function every time.
i suppose should give more context explain why i'm doing this. objects created via overloaded new operator, , needs object know context created in (lua state). e.g.
foo* object = new(l) foo(...); // foo derived apiobject, , want apiobject have reference l
currently done in unelegant way - new operator allocates additional bytes before object , stores l pointer in there, along additional data describe object type. base class receives pointer 'metadata' via init function.
otherwise, first thing comes mind virtual functions, can't called constructor, i'd have register base apiobject
pointer call virtual function @ later point, , i'm not sure that's prettier current implementation.
what type required registerobject
? if takes base*
, can call constructor of base
, regardless of final hierarchy. if takes other type, want call constructor of type; not want call classes derived base
, derived whatever type takes.
if registerobject
takes base*
, , call function in derived class, first thing occur pointer pass converted base*
. registerobject
never receives pointer derived object, base
in derived object.
Comments
Post a Comment