In Delphi methods of a class can be called without creating an instance of the class at first. That's similar to C++ static methods. But in C++ it is not possible to assign classes as values to variables and then to create instances of the class by calling a virtual constructor function from such a class reference.

For C++Builder the __classid function is a special extension, to get class references. A small Delphi unit has to be added to the C++Builder project, to create class instances from such a class reference.

To do the same with other compilers is much more difficult.

As for C++Builder where a class TMetaClass is defined, for other compilers such a class is defined too. In addition the type TClass is defined as a pointer to TMetaClass:

typedef TMetaClass* TClass

TMetaClass is the class reference type for TObject and it is the base class of all class reference types of all other classes. These class references are defined as instances of a class ClassRef, which is a generic class:

template <typename Class>
class ClassRef

where the template parameter denotes the original class. That way for a hierarchy of classes, which are derived one from another, there is a parallel hierarchy of class references. The class references are implemented as singletons and only created, if needed. The exact definition of the ClassRef class is tricky and works only, because DelphiXE2Cpp11 also inserts some additional helper code into every class declaration. The following code demonstrates how a small class factory using class references is converted from Delphi to C++:

 TBase = class                           
  function GetName: String; virtual;     

 TBaseClass = class of TBase;            

 TDerived = class(TBase)                
  function GetName: String; override;    

 TDerivedClass = class of TDerived;    


function make(Base: TBaseClass): TBase;  
  result := Base.Create;                 

function testTactory: boolean;           
 s : String;                             
 p : TBase;                              
 p := make(TDerived1);                   
 result := p.GetName = 'TDerived1';      


class TBase : public System::TObject                                                                  
  typedef System::ClassRef ClassRefType;  
  ClassRefType* ClassType() const {return  System::class_id();}
  TBase* Create() {return new TBase();}
  static TBase* SCreate() {return new TBase();}
  System::String ClassName() {return L"TBase";}
  static System::String SClassName() {return L"TBase";}

typedef TBase::ClassRefType TBaseClass;                                                              

class TDerived : public TBase                                                                       
  typedef System::ClassRef ClassRefType;  
  ClassRefType* ClassType() const {return  System::class_id();}
  TDerived* Create() {return new TDerived();}
  static TDerived* SCreate() {return new TDerived1();}
  System::String ClassName() {return L"TDerived";}
  static System::String SClassName() {return L"TDerived";}

typedef TDerived::ClassRefType TDerivedClass;    

TBase* make(TBaseClass* Base)                                          
  TBase* result = nullptr;                                              
  result = Base->Create();                                               
  return result;                                                         

bool testfactory()                                                       
  bool result = false;                                                   
  String s;                                                              
  TBase* P = nullptr;                                                   
  P = make(class_id());            
  result = P->GetName() == L"TDerived";                                
  return result;                                                         

The central point in this code is the call of the class_id-function:

P = make(class_id());

The class_id-function fulfills the same purpose as the __classid-function in C++Builder code: it delivers class reference. In the example the class_id-function delivers the class reference to the class TDerived.

   deutsch Deutsch


Latest News
Delphi2Cpp 2.2: Facilitated translation of incomplete code [more...]

Delphi2Cpp 2.1: Delphi 10.3 inline variables [more...]

"Thanks for your great work, really appreciate the work you have done on zlib and compiling ... test case."

Mattewada, Udayabhaskar
Nokia India 02/01/2021

[from case study...]

"A masterpiece -- Delphi2Cpp has exceeded all my expectations by far."

Tony Hürlimann
virtual-optima 08/20/2011

"First off, I have to say WOW! Delphi2Cpp is doing a *fantastic* job!"

Daniel Flower
linkrealms 01/15/2011

This website is generated from plain text with [Minimal Website ]

Minimal Website
Minimal Website is made with TextTransformer

TextTransformer is made with Borland CBuilder