class-reference type

Top  Previous  Next

What is translated > class-reference type

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. This is possible in Delphi however, as shown in the following example code:


 TBase = class



 TBaseClass = class of TBase;


 TDerived = class(TBase)



 TDerivedClass = class of TDerived;



function make(Base: TBaseClass): TBase;


  result := Base.Create;  // will create TBase or TDerived in dependence of the passed parameter




The variables TBaseClass and TDerivedClass are called "class references" of TBase or TDerived respectively.C++Builder has a special extension, which allows the creation of class references, but the creation of class instances from them isn't possible, only some other class functions can be called from them.


With Delphi2Cpp the code above can be translated. The way of translation is different for C++Builder and other compilers.

A creation of class instances from class references is possible only, if the class has a standard constructor.



Alternatively also the macros DECLARE_DYNAMIC and  IMPLEMENT_DYNAMIC might help.


This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content