954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to call constructor for descendant of TObject ?

At runtime I obtain an instance of descendant of TObject (for example its name is TGoodForm).
The definition of TGoodForm is unknown at compile time (I know that it is TGoodForm from TObject::ClassName()).
The question: how to create new instance of TGoodForm ?
In another words, can I get address of constructor through reference to existing object at runtime?
(Assembler is permitted)

admdvv
Newbie Poster
6 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 
At runtime I obtain an instance of descendant of TObject (for example its name is TGoodForm). The definition of TGoodForm is unknown at compile time (I know that it is TGoodForm from TObject::ClassName()). The question: how to create new instance of TGoodForm ? In another words, can I get address of constructor through reference to existing object at runtime? (Assembler is permitted)

Can you post code by chance?

subtronic
Junior Poster
117 posts since Aug 2003
Reputation Points: 44
Solved Threads: 1
 
for(int i=0, n=form->ComponentCount; i<n; ++i){
    TComponent* comp=form->Components[i];
    if(comp->ClassInfo()=="TGoodForm"){
        // here I want to create new instance of the same class
    }
}


<< moderator edit: added [code][/code] tags >>

admdvv
Newbie Poster
6 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Having address of constructor, I'll write:

typedef TObject* __fastcall (*Ctor)(TComponent* owner);
TObject* newObject=(*(Ctor)address)(form);


<< moderator edit: added [code][/code] tags >>

admdvv
Newbie Poster
6 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

Maybe I'm not understanding you correctly..

Generated: Thu May 12 16:17:59 2005

[<strong>001</strong>]     for (int i = 0, n = form->ComponentCount; i < n; ++i) {
[<strong>002</strong>]             TComponent *comp = form->Components[i];
[<strong>003</strong>]             if (comp->ClassInfo() == "TGoodForm") {
[<strong>004</strong>]                     <strong>TGoodForm *tf = new TGoodForm();        // ????</strong>
[<strong>005</strong>]             }
[<strong>006</strong>]     }
subtronic
Junior Poster
117 posts since Aug 2003
Reputation Points: 44
Solved Threads: 1
 

But I have no declaration of class TGoodForm (I get instance from runtime library), and compiler doesn't know about TGoodForm::TGoodForm.
But I found the solution:

The solution is to create delphi function which calls virtual constructor

// delphi unit ComponentCreator.pas

unit ComponentCreator;
interface
uses Classes;
function ComponentCreate(cls: TComponentClass; Owner: TComponent): TComponent;
implementation
function ComponentCreate(cls: TComponentClass; Owner: TComponent): TComponent;
begin
ComponentCreate:=cls.Create(Owner);
end;
end.

// C++ unit

#include "ComponentCreator.hpp"

TClass newClass=existingComponent->ClassType();
RegisterClass(newClass);
TComponent* newComponent=ComponentCreate(newClass, form);
UnRegisterClass(newClass);
newComponent->Name=newName;

// It was useful to analyze VCL source file "Classes.pas"

admdvv
Newbie Poster
6 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You