abstract methods

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > abstract methods

Like Delphi, C++ also supports abstract methods. The most natural translation looks like this:

 

function Get(Index: Integer): Integer; virtual; abstract;

 

 

becomes

 

virtual int __fastcall Get(int Index) = 0;

 

However, unlike Delphi, C++ does not allow creating objects from classes that still contain abstract methods. In fact, the compiler will reject such code at compile time.

During development, it can sometimes be practical if this code also compiles and runs in C++. For this reason, Delphi2Cpp offers an option to generate minimal function bodies for abstract methods.

 

Behavior before Delphi2Cpp 2.6.0

 

Earlier versions (before 2.6.0) translated the example into:

 

virtual int __fastcall Get(int Index){return 0;} // = 0;

 

This was intended only as a temporary workaround.

 

Behavior since Delphi2Cpp 2.6.0

 

From version 2.6.0 onward, the translation looks like this:

 

virtual int __fastcall Get(int Index){ThrowAbstractMethodError(L"[classname}::Get"); return 0;};

 

This makes the behavior in C++ identical to Delphi:

 

The class can still be instantiated, even though it has abstract methods.
As soon as such a method is called, an error is thrown.

 

This output is now generated regardless of the option to make classes non-abstract. In future versions, the option will be removed completely.

 

 

Remark

 

There are special cases where generated C++ code may call an abstract method that Delphi itself does not call.

For example, a base class constructor might call a method that is only implemented (non-abstract) in its derived classes.

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content