Self instance

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > class 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;

 

 

DelphiXE2C++11 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 DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content