Recent

Author Topic: Constructor not called when calling classvar.Create()  (Read 3428 times)

SirTwist

  • New Member
  • *
  • Posts: 21
Constructor not called when calling classvar.Create()
« on: February 24, 2019, 11:46:38 am »
Hi folks,

I have a variable class_type: TClass, and I'm calling class_type.Create; This does allocate the object but is not invoking the Create() method of this object.

Very simplified:
Code: Pascal  [Select][+][-]
  1. type
  2.   TmyObj = class
  3.   public
  4.     constructor   Create();
  5.   end;
  6.   TmyObjClass = class of TmyObj;
  7.  
  8. var
  9.   class_type: TmyObjClass;
  10.   obj: TMyObj;    
  11. begin
  12.   class_type := TmyObj;
  13.   obj := class_type.Create;
  14. end;
  15.  
Any ideas how I can bring FP to call the constructor?
FreePascal 3.04 with Lazarus 1.8

Many thanks in advance!
Kind regards,
Sir Twist

Thaddy

  • Hero Member
  • *****
  • Posts: 18911
  • Glad to be alive.
Re: Constructor not called when calling classvar.Create()
« Reply #1 on: February 24, 2019, 01:40:44 pm »
That's too simplified. How did you implement the constructor? Did you call inherited create? etc..

Example that works:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. type
  3.   TmyObj = class
  4.   public
  5.     constructor   Create;virtual;
  6.   end;
  7.   TmyObjClass = class of TmyObj;
  8.  
  9.   constructor TMyObj.Create;
  10.   begin
  11.     inherited;
  12.     writeln('called');// proof it gets called
  13.   end;
  14. var
  15.   class_type: TmyObjClass;
  16.   obj: TMyObj;    
  17. begin
  18.   class_type := TmyObj;
  19.   obj := class_type.Create;
  20.   obj.free;
  21. end.

« Last Edit: February 24, 2019, 02:10:19 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12288
  • Debugger - SynEdit - and more
    • wiki
Re: Constructor not called when calling classvar.Create()
« Reply #2 on: February 24, 2019, 01:45:38 pm »
What exactly is it NOT calling?

It should call TmyObj.Create().
But it will not call TInhertiedFromMyObj.Create().

And that is correct for your code.

If you want that for inherited, you need TmyObj.Create() to be virtual, and override it in inherited classes.
« Last Edit: February 24, 2019, 01:48:13 pm by Martin_fr »

Thaddy

  • Hero Member
  • *****
  • Posts: 18911
  • Glad to be alive.
Re: Constructor not called when calling classvar.Create()
« Reply #3 on: February 24, 2019, 01:46:42 pm »
martin, posts crossed. I added an example along these lines: and it simply works.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

SirTwist

  • New Member
  • *
  • Posts: 21
Re: Constructor not called when calling classvar.Create()
« Reply #4 on: February 24, 2019, 03:33:24 pm »
I'm so sorry. Yes, you are right, I simplified it too much. And yes, it is working, if I'm doing it right.
My fault was, that the original code had
Code: Pascal  [Select][+][-]
  1. var
  2.   class_type: TClass;
  3.  
and TClass obviously has no "constructor Create; virtual"...
I have to check my old Delphi code, from where I migrated that whole unit. But maybe I changed to much...
Thanks for bringing me back on line.

Thaddy

  • Hero Member
  • *****
  • Posts: 18911
  • Glad to be alive.
Re: Constructor not called when calling classvar.Create()
« Reply #5 on: February 24, 2019, 03:37:20 pm »
Just take that old delphi code and simply add {$ifdef fpc}{$mode delphi}{$endif} on top of the units and the program. If it is pre D2009 code that is usually enough to get it working. Usually means almost always...

From the command line you can also do fpc -Mdelphi -B <yourdpr.dpr>  which will compile all available units in the project as Delphi mode. (providing the paths are correct that doesn even need the above additions, although I'd recommend it to make it compiler agnostic.)

For Visiual projects with lazarus, it is slightly different, but there is a converter wizzard.
Non-visual units need just the {$mode delphi} except for 32-64 bit issues but these are he same for delphi.
« Last Edit: February 24, 2019, 04:09:13 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018