Self instance

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > class methods > Other compilers cllass methods > Self instance

Like the "this" pointer in C++ is an implicit parameter to all member functions, in Delphi the "Self" instance is an implicit parameter to class functions. Other class methods can be called there through this instance and they can be called by hidden use of "Self". "Self" must not appear in the code. For example:

 

 

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

begin

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

  begin

    Init;

    Done;

    Free;

  end;

  result := xi;

end;

 

 

Delphi2Cpp can convert this code adequately only, if the option to create meta classes is enabled. The code then becomes to:

 

 

/*#static*/ int TBase::ClassMethod(int xi)

{

  int result = 0;

  /*# with Create do */

  {

    auto with0 = SCreate();

    with0->Init();

    with0->Done();

    delete with0;

  }

  result = xi;

  return result;

}

 

"SCreate" is a static method, which returns a pointer to a new instance of TBase.



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content