Delphi 7: create a new instance of an unknown object -



Delphi 7: create a new instance of an unknown object -

i have tobject reference instance of unkown class. how phone call constructor of unknown class create instance of it? know delphi has rtti, isn't clear how utilize it.

you can't build object of unknown type. compiler has know right class type @ compile-time can generate proper code. if constructor requires parameters? how many? info types? passed stack or registers? info important.

that beingness said, if classes in question derived mutual base of operations class has virtual constructor, , can build such objects. can utilize tobject.classtype() method reference class type of existing object, type-cast base of operations class type, , phone call constructor. example:

type tbase = class public constructor create(params); virtual; end; tbaseclass = class of tbase; tderived1 = class(tbase) public constructor create(params); override; end; tderived2 = class(tbase) public constructor create(params); override; end; ...

var newobj: tbase; begin if someobj tbase newobj := tbaseclass(someobj.classtype).create(params); end;

delphi delphi-7 rtti

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -