| | |
How to call constructor for descendant of TObject ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2005
Posts: 6
Reputation:
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)
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)
•
•
•
•
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)
•
•
Join Date: May 2005
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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 } }
•
•
Join Date: May 2005
Posts: 6
Reputation:
Solved Threads: 0
Having address of constructor, I'll write:
<< moderator edit: added [code][/code] tags >>
C++ Syntax (Toggle Plain Text)
typedef TObject* __fastcall (*Ctor)(TComponent* owner); TObject* newObject=(*(Ctor)address)(form);
Maybe I'm not understanding you correctly..
Generated: Thu May 12 16:17:59 2005
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] }
•
•
Join Date: May 2005
Posts: 6
Reputation:
Solved Threads: 0
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"
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"
![]() |
Similar Threads
- Doubts about constructor (C++)
- constructor problem (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Error LNK2022: Metadata Operation Failed (8013118D)
- Next Thread: Infinite Loop in the switch statements
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





