Problems with constructors

Top  Previous  Next

What is translated > 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 to 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 cannot recognize this.

 

 

There is still another problem with special constructors. 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 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 );

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content