Problems with constructors

Top  Previous  Next

What is translated > Types > Records, Classes, Interfaces > Class > Constructors > Problems with constructors

 

Summarizing, there remain two problems for which the translated constructors have to be checked:

 

1.the order of construction of the derived and the base classes is differently in Delphi and C++
2.member variables should be initialized in at the beginning of the constructor code in the initialization list. But sometimes the value can depend on other calculations and  Delphi2Cpp IIcannot recognize this.
3.In Delphi there can be several constructors with the same signature but with different names.. E.g.:

 

 

TCoordinate = class(TObject)

public

  constructor CreateRectangular(AX, AY: Double);

  constructor CreatePolar(Radius, Angle: Double);

private

  x,y : Double;  

end;

 

 

constructor TCoordinate.CreateRectangular(AX, AY: Double);

begin

  x := AX;

  y := AY;

end

 

constructor TCoordinate.CreatePolar(Radius, Angle: Double);

begin

  x := Radius * cos(Angle);

  y := Radius * sIn(Angle);

end

 

After translation the two constructors become ambiguous: 

 

__fastcall TCoordinate::TCoordinate( double AX, double AY )

 : x(AX),

   y(AY)

{

}

 

__fastcall TCoordinate::TCoordinate( double Radius, double Angle )

 : x(Radius * cos( Angle )),

   y(Radius * sin( Angle ))

{

}

 

They not only have the same signature now, but also the same name. In such cases the conflict has to be avoided manually. For example you can remove the second constructor and define a static function instead:

 

 

TCoordinate* __fastcall TCoordinate::CreatePolar( double Radius, double Angle )

{

  return new TCoordinate(Radius * cos( Angle ), Radius * sin( Angle )

}

 

At all positions, where the second constructor shall be used, the new function has to be used instead. This positions can be found easily, because Delphi2Cpp II inserts the original name of the constructor as a comment into the translated code, if the Verbose option is enabled and if the name is not written exactly as "Create".

 

 

P = /*CreatePolar*/ new TCoordinate( AX, AY );

 

->

 

P = TCoordinate::CreatePolar( AX, AY );

 

 

 

4. Calls of virtual constructors are allowed in Delphi, but in C++ such calls should be avoided in C++

 

As example the TPolygon class and the derived classes TTriangle and TSquare as defined here

 

http://www.delphibasics.co.uk/RTL.asp?Name=Abstract

 

can be taken:

 

  TPolygon = class

  ...

  protected

     procedure setArea; Virtual; Abstract;  // Cannot code until sides known

  ...

 

  TTriangle = class(TPolygon)

  protected

    procedure setArea; override;   // Override the abstract method

 

 

  TSquare = class(TPolygon)

  protected

    procedure setArea; override;   // Override the abstract method

 

 

constructor TPolygon.Create(sides, length : Integer);

begin

  ...

  setArea;

end;

 

 

In Delphi the setAres-procedure of the derived classes will be called in their constructors. With C++Builder this works well too, but other C++ compilers always try to call the setArea-procedure of TPolygon. Manual post-processing is necessary then. E.g. setArea could be made accessible and called after construction:

 

triangle = new TTriangle(3, 10);

triangle->setArea();

 

 

 

 



This page belongs to the DelphiXE2Cpp11 Documentation

DelphiXE2Cpp11 home  Content