Constructors with the same signature

Top  Previous  Next

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

 

A big problem with constructors is, that 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 strict translation the two constructors would 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 ))

{

}

 

Therefore Delphi2Cpp inserts a second parameter into the alphabetically second declaration to distinguish the constructors in C++.

 

 

class TCoordinate : public System::TObject

{

public:

  __fastcall TCoordinate(double AX, double AY, void* OverloadSelector0);

  __fastcall TCoordinate(double Radius, double Angle);

private:

  double x;

  double y;

};

 

__fastcall TCoordinate::TCoordinate(double AX, double AY, void* OverloadSelector0)

 : x(AX),

   y(AY)

{

}

 

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

 : x(Radius * cos(Angle)),

   y(Radius * sIn(Angle))

{

}

 

The following code snippet demonstrates how the constructors are called:

 

 

procedure TestConstructors;

var

  c1, c2 : TCoordinate;

begin

  c1 := TCoordinate.CreateRectangular(1.0, 2.0);

  c1 := TCoordinate.CreatePolar(1.0, 90.0);

 

becomes to:

 

void __fastcall TestConstructors()

{

  TCoordinate* c1 = nullptr;

  TCoordinate* c2 = nullptr;

  c1 = new TCoordinate(1.0, 2.0, nullptr);

  c1 = new TCoordinate(1.0, 90.0);

 

 

 

 



This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content