Re: Override constructor in class helper?

Mattias Gaertner via fpc-pascal <[email protected]> Thu, 27 Nov 2025 19:53:46 +0100
Newsgroups gmane.comp.compilers.free-pascal.general
Message-ID <[email protected]>

On 11/27/25 11:24, Thomas Kurz via fpc-pascal wrote:
> Hello,
> 
> what I'm trying to do is something like this:
> 
> TStringStreamHelper = class helper for TStringStream
>    constructor Create;
>    constructor Create (const AString: AnsiString);
> end;
> 
> constructor TStringStreamHelper.create;
> begin
>    inherited Create ('', TEncoding.UTF8);
> end;
> 
> constructor TStringStreamHelper.create (const AString: AnsiString);
> begin
>    inherited Create (AString, TEncoding.UTF8);
> end;
> 
> Both ChatGPT and Gemini agree that this won't work because FPC resolves Create in this order:
> 1. Look at the real class (TStringList) for a matching constructor.
> 2. Only if no such method exists, check helpers in scope for a matching name.

I hope you did not pay them for that answer.


> However, if I try, the helper constructor *is* invoked.
> 
> Because I'm on FPC 3.3.1 trunk, I wonder whether this is indended behavior and I can rely on it in the future, or whether this is a bug and will break in the final 3.4.0 release.

Intended.

Mattias

_______________________________________________
fpc-pascal maillist  -  [email protected]
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal