Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-11

Overloading problem (converting Delphi -> FPC)

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Birger Jansen

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

Reply by email

Sven Barth
to Birger Jansen
Birger Jansen wrote 55 lines
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.

According to your description how Delphi handles it: don't you mean "highest" possible constructor? The lowest one would be constructor B in my opinion...

Regards,
Sven

Reply by email

Sven Barth
to Sven Barth
Sven Barth wrote
According to your description how Delphi handles it: don't you mean "highest" possible constructor? The lowest one would be constructor B in my opinion...

On secord thought it all depends where you see the root of the class tree... at the top or at the bottom :P

Regards,
Sven

Reply by email

Birger Jansen
to Sven Barth

My trees are 'roots at the top'. Which is quite unnatural, I agree.

-----Oorspronkelijk bericht-----
Van: Sven Barth
Verzonden: do 04-11-2010 10:11
Aan: ;
Onderwerp: Re: [Lazarus] Overloading problem (converting Delphi -> FPC)

Show quoted text 17 lines
Am 04.11.2010 10:08, schrieb Sven Barth: > According to your description how Delphi handles it: don't you mean > "highest" possible constructor? The lowest one would be constructor B in > my opinion... On secord thought it all depends where you see the root of the class tree... at the top or at the bottom :P Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email