Initialization/Finalization

Top  Previous  Next

What is translated > Statements > Initialization/Finalization

 

There isn't any direct counterpart for the sections "initialization" and "finalization" of a unit in C++. These sections are therefore translated as two functions which contain the respective instructions. In addition, a global variable of a class is defined. In the constructor of this class the initialization routine is called and in destructor the routine for the finalization is called. When the program is started, the global variables are created at first and therefore also the units are initialized.

 

 

initialization

 

pTest := CTest.Create;

 

finalization

 

pTest.Free();

 

 

->

 

 

void Tests_initialization()

{

  pTest = new CTest;

}

 

void Tests_finalization()

{

  delete pTest;

}

 

class test_unit

{

public:

  test_unit()

  {

    test_initialization();

  }

  ~  test_unit(){test_finalization(); }

};

test_unit _test_unit;

 

 

This however is only the short version. Sometimes the initialization have to be executed in a special order: one initialization can require another. Such cases have to be corrected by the developer, but Delphi2Cpp already generates code that allows these corrections to be made easily. The declarations:

 

void Tests_initialization();

void Tests_finalization();

 

are put into the header and there is an additional static boolean variable, that prevents that the initialization or finalization is executed twice. E.g.:

 

 

static bool Test_Initialized = false;

 

void Tests_initialization()

{

if(Test_Initialized)

   return;

Test_Initialized = true;

...

 

So in the normal case the initializations and finalizations keep being executed automatically, but the developer also may easily call the according functions from another unit, to force a special order of initialization or finalization.

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content