Recent

Author Topic: constructor issue  (Read 2170 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1289
constructor issue
« on: March 19, 2018, 12:29:50 pm »
Hi,

I'd like to define a descendant class of TStringList. Here, I can define constructor, and inherit within it.  ie, following code compiles fine.

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass=class(TStringList)
  3.       constructor Create;
  4.   end;
  5.  
  6. constructor Create;
  7. begin
  8.     inherited;
  9. end;

But I cannot add override after the "Create".  Following codes issue compilation error saying "there are no constructor to override".   Is this natural? and What is the logic?

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyClass=class(TStringList)
  3.       constructor Create; override;
  4.   end;
  5.  


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9857
  • Debugger - SynEdit - and more
    • wiki
Re: constructor issue
« Reply #1 on: March 19, 2018, 12:37:04 pm »
Because the base class (TStringList) does not define the constructor as virtual.

Only virtual methods (or constructors) can be overridden.

You can re-introduce it in your class, and make it virtual, and then if you inherhit from that class of yours, you can override

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: constructor issue
« Reply #2 on: March 19, 2018, 01:00:14 pm »
The ultimate base class: Tobject.create is not virtual. Base constructors are not virtual.
Use reintroduce, then call inherited
https://www.freepascal.org/docs-html/rtl/system/tobject.html
Of course that is in the manual.....
Note that it does nothing (right now) so there is no need to call inherited, just mark it as  reintroduce
Example:
Code: Pascal  [Select][+][-]
  1. program re;
  2. {$mode delphi}
  3. type
  4.   TMyCustomclass = class
  5.   public
  6.     constructor create;virtual;reintroduce;
  7.   end;
  8.   TMyClass = class(TMyCustomClass)
  9.   public
  10.     constructor Create;override;
  11.   end;
  12.   constructor TMyCustomclass.Create;
  13.   begin
  14.     inherited create; // at the moment Tobject.create does nothing. Will generate a warning, though.
  15.     writeln('Is this what you mean?');
  16.   end;
  17.   constructor TMyClass.Create;
  18.   begin
  19.     inherited;
  20.   end;
  21.  
  22. var
  23.   a:TMyClass;
  24. begin
  25.   a := TMyClass.Create;
  26.   a.free;
  27. end.
   

Note since your constructor create is now virtual you can override it in derived classes.

« Last Edit: March 20, 2018, 08:34:54 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

egsuh

  • Hero Member
  • *****
  • Posts: 1289
Re: constructor issue
« Reply #3 on: March 20, 2018, 06:41:56 am »
I see. Thank you very much.

So following code just calls TObject.create.

  AStringList := TStringList.Create; 


Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: constructor issue
« Reply #4 on: March 20, 2018, 08:27:31 am »
Yes - as per documentation - but the compiler also triggers things like NewInstance and InitInstance when it sees the create. And so allocates the proper class. These are called before the actual create is called.
My example code shows that it is better, but at the moment not necessary, to call the inherited static create from the virtual create. This may change in the future, but not likely, since NewInstance can be overriden too because it is virtual. https://www.freepascal.org/docs-html/rtl/system/tobject.newinstance.html
But if you need a parameterless virtual constructor, that's how it can be done.
« Last Edit: March 20, 2018, 08:39:50 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018