Recent

Author Topic: [SOLVED] Override constructor method  (Read 6035 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
[SOLVED] Override constructor method
« on: August 18, 2017, 10:37:39 am »
hi All,
on Lazarus 1.8RC4 i am getting a warning regarding a constructor as per attached.
if i am trying to go with the warning and place the suggested override i get an error.
please advise if this is the intended behavior.

thank you
« Last Edit: August 18, 2017, 11:58:46 am by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Override constructor method
« Reply #1 on: August 18, 2017, 10:41:26 am »
The ancestor Constructor Create has a different set of parameters.
You may just ignore the warning or create an overloaded Constructor Create(AOwner: TComponent);

e.g.
Code: Pascal  [Select][+][-]
  1. type TQImage = class(TImage)
  2. public
  3.   Constructor Create(AOwner: TComponent); override;
  4.   Constructor Create(AOwner: TComponent; ALeft: integer);
  5.   procedure SomeConstructor;
  6. end;
  7.  
  8. Constructor TQImage.Create(AOwner: TComponent);
  9. begin
  10.   inherited Create(AOwner);
  11.   Left := 0;
  12.   SomeConstructor;
  13. end;
  14.  
  15. Constructor TQImage.Create(AOwner: TComponent; ALeft: integer);
  16. begin
  17.   inherited Create(AOwner);
  18.   Left := ALeft;
  19.   SomeConstructor;
  20. end;
« Last Edit: August 18, 2017, 10:48:16 am by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Override constructor method
« Reply #2 on: August 18, 2017, 10:42:29 am »
if i am to ignore the warning isn't it a bug?
Lazarus 2.0.2 64b on Debian LXDE 10

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Override constructor method
« Reply #3 on: August 18, 2017, 10:47:49 am »
if i am to ignore the warning isn't it a bug?
Not yet a bug. Nothing will work incorrectly if you do. However, it's not "clean" :)
If you want it clean, do as I've given the example above (make two constructors - one to "override" the TImage constructor, and another - a new one).
Maybe, there are other ways to.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Override constructor method
« Reply #4 on: August 18, 2017, 11:20:23 am »
Another way to suppress the warning when you reuse the same name (Create) for a non-virtual constructor in a descendant is to use the word "reintroduce".
This tells the compiler that you intended to introduce a new constructor with the same name as an inherited constructor. Yet you still have access to the original constructor through use of the modifier "inherited".

Code: Pascal  [Select][+][-]
  1. constructor TQImage.Create(AOwner: TComponent; ALeft: Integer); reintroduce; // suppresses a compiler warning
  2. begin
  3.   inherited Create(AOwner);
  4.   Left:=ALeft;
  5.   ...
  6. end;

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Override constructor method
« Reply #5 on: August 18, 2017, 11:58:19 am »
now i got it.
thank you!
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018