non virtual class methods

Top  Previous  Next

What is translated > class methods > non virtual class methods

 

The translation of a definition of a non virtual class method looks like:

 

int __fastcall /*static*/ TBase::ClassMethod( const TBase* xpTHIS, int xi ) {

  int result = 0;

  const TBase* pTHIS = (TBase*) xpTHIS;

  ...

  return result;

}

 

pTHIS represents Delphi's class Self. In the C++ translation a pointer to an  instance of the class is passed as parameter from outside of the method and assigned to pTHIS. The cast from xpTHIS to pTHIS doesn't do anything here. It's just a formal line of code, which is repeated in all class functions and which is necessary in cases, where the class function is virtual.

The code which is indicated with "..." is interpreted as an implicit with statement:

 

with pTHIS do

  ...

 

A complete example demonstrates this:

 

class function TBase.ClassMethod(xi: Integer): Integer;

begin

  with Create do  <-- new object from a virtual constructor

  begin

    Init;

    Done;

    Free;

  end;

  result := xi;

end;

 

The explicit with statement "with Create do" is part of the implicit with statement, so that Create is called with pTHIS.

 

int __fastcall /*static*/ TBase::ClassMethod( const TBase* xpTHIS, int xi ) {

  int result = 0;

  const TBase* pTHIS = (TBase*) xpTHIS;

  /*# with Create do */

  {

    TBase* with0 = (TBase*) pTHIS->Create();  <-  implicit with

    {

      with0->Init();

      with0->Done();

      delete with0;

    }

  }

  result = xi;

  return result;

}

 

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content