944,039 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3594
  • C++ RSS
May 12th, 2005
0

How to call constructor for descendant of TObject ?

Expand Post »
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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
admdvv is offline Offline
6 posts
since May 2005
May 12th, 2005
0

Re: How to call constructor for descendant of TObject ?

Quote 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?
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 12th, 2005
0

Re: How to call constructor for descendant of TObject ?

C++ Syntax (Toggle Plain Text)
  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
admdvv is offline Offline
6 posts
since May 2005
May 12th, 2005
0

Re: How to call constructor for descendant of TObject ?

Having address of constructor, I'll write:
C++ Syntax (Toggle Plain Text)
  1. typedef TObject* __fastcall (*Ctor)(TComponent* owner);
  2. TObject* newObject=(*(Ctor)address)(form);
<< moderator edit: added [code][/code] tags >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
admdvv is offline Offline
6 posts
since May 2005
May 12th, 2005
0

Re: How to call constructor for descendant of TObject ?

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]     }
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 14th, 2005
0

Re: How to call constructor for descendant of TObject ?

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"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
admdvv is offline Offline
6 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error LNK2022: Metadata Operation Failed (8013118D)
Next Thread in C++ Forum Timeline: Infinite Loop in the switch statements





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC