virtual class methods

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > class methods > virtual class methods

Because there are no virtual static methods in C++ DelpiXE2Cpp11 has an option, which allows to convert virtual class methods either to static non-virtual methods or to virtual non-static methods.

 

The first case results into the same code as for non-virtual class methods. If the virtual class methods aren't overridden, this is obviously the best option. But if the methods are overwritten, the virtual class methods have to be converted to virtual C++ methods. Then these methods cannot be called through an class type expression in C++ any more, If they are called that way in the Delphi code, an adequate instance of the class has to be provided in C++. If the option to create meta classes is enabled Delphi2Cpp II provides these instances automatically:

 

 

TBase = class(TObject)

public

  class function ClassVirtual(xi: Integer): Integer; virtual;

 

 

var

  base : TBase;

begin

  base.ClassMethod(0);

  TBase.ClassVirtual(0);

  TDerived.ClassVirtual(0);

 

->

 

base->ClassMethod(0);

TBase::ClassRefType::getClassInstance()->ClassVirtual(0);

TDerived::ClassRefType::getClassInstance()->ClassVirtual(0);

 

 

By calling ClassVirtual through the TBase pointer base, the correct version of  ClassVirtual will be called as for for non-static methods too. The correct version of ClassVirtual will be called through class references too, because the static function ClassRefType delivers the class reference of TBase or TDerived and the call of getClassInstance delivers a singleton instance of a pointer to TBase or TDerived respectively. 

 

 

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content