[Lazarus] Overloading problem (converting Delphi -> FPC)

Birger Jansen birger at cnoc.nl
Thu Nov 4 10:04:25 CET 2010


Hi all,

While converting a project from Delphi to FPC I stumple upon a problem. I hope some of you can help me with this!

It seems that FPC has problems with finding the right overloaded constructor when the various overloaded functions take class parameters and these classes are related to each other.

An example will make it easier to explain:

----------------------------------------------------------
program Project1;

{$APPTYPE CONSOLE}

uses
  Classes, contnrs;

type
  TBase = class(TObject);
  TSubA = class(TBase);
  TSubB = class(TSubA);

  TBaseClass = class of TBase;
  TSubAClass = class of TSubA;

  { TFunc }

  TFunc = class
  public
    constructor Create(const AClass: TSubAClass);overload; {constructor A}
    constructor Create(const AClass: TBaseClass);overload; {constructor B}
  end;

var
  t1, t2, t3: TFunc;

constructor TFunc.Create(const AClass: TSubAClass); {constructor A}
begin
end;

constructor TFunc.Create(const AClass: TBaseClass); {constructor B}
begin
end;

begin
  t1 := TFunc.Create(TBase);
  t2 := TFunc.Create(TSubA);
  t3 := TFunc.Create(TSubB);
end.
----------------------------------------------------------

This runs in Delphi. For t1, Constructor B is called. For t2 and t3 Constructor A is called. The compiler just takes the 'lowest' possible constructor for t3. 
However, in FPC the compiler doesn't know what constructor to use do and compilation fails with a "Can't determine which overloaded function to call" for t3.

Why doesn't the compiler use the lowest possible constructor for t3? Is there some way to force this?
This constructor (one of the different overloaded versions) is called from a lot of different places in the code, I hope there is come way to fix this without rewriting it all.

Kind regards,
  Birger




More information about the Lazarus mailing list