How to call constructor for descendant of TObject ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 6
Reputation: admdvv is an unknown quantity at this point 
Solved Threads: 0
admdvv admdvv is offline Offline
Newbie Poster

How to call constructor for descendant of TObject ?

 
0
  #1
May 12th, 2005
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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: How to call constructor for descendant of TObject ?

 
0
  #2
May 12th, 2005
Originally Posted by admdvv
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?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: admdvv is an unknown quantity at this point 
Solved Threads: 0
admdvv admdvv is offline Offline
Newbie Poster

Re: How to call constructor for descendant of TObject ?

 
0
  #3
May 12th, 2005
  1. for(int i=0, n=form->ComponentCount; i<n; ++i){
  2. TComponent* comp=form->Components[i];
  3. if(comp->ClassInfo()=="TGoodForm"){
  4. // here I want to create new instance of the same class
  5. }
  6. }
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: admdvv is an unknown quantity at this point 
Solved Threads: 0
admdvv admdvv is offline Offline
Newbie Poster

Re: How to call constructor for descendant of TObject ?

 
0
  #4
May 12th, 2005
Having address of constructor, I'll write:
  1. typedef TObject* __fastcall (*Ctor)(TComponent* owner);
  2. TObject* newObject=(*(Ctor)address)(form);
<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: How to call constructor for descendant of TObject ?

 
0
  #5
May 12th, 2005
Maybe I'm not understanding you correctly..

Generated: Thu May 12 16:17:59 2005
[001]     for (int i = 0, n = form->ComponentCount; i < n; ++i) {
[002]             TComponent *comp = form->Components[i];
[003]             if (comp->ClassInfo() == "TGoodForm") {
[004]                     TGoodForm *tf = new TGoodForm();        // ????
[005]             }
[006]     }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 6
Reputation: admdvv is an unknown quantity at this point 
Solved Threads: 0
admdvv admdvv is offline Offline
Newbie Poster

Re: How to call constructor for descendant of TObject ?

 
0
  #6
May 14th, 2005
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"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC