Recent

Author Topic: Abstract classes  (Read 15630 times)

totya

  • Hero Member
  • *****
  • Posts: 640
Re: Abstract classes
« Reply #15 on: March 28, 2018, 06:25:24 pm »
What does it mean, "doesn't work"?

Hi!

As I said, I create simple demo. I can't get any compiler warnings:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.lfm}
  25.  
  26. uses
  27.   Unit3;
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33.   DataModule1.MyShow;
  34. end;
  35.  
  36. end.
  37.  

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil,
  9.  
  10.   Dialogs;
  11.  
  12. type
  13.   TCustomDataModule = class(TDataModule)
  14.   protected
  15.     procedure ImportantAbstractProcedureNeedToOverrideButNothingHappen; dynamic; abstract;
  16.   public
  17.     procedure MyShow;
  18.   end;
  19.  
  20. var
  21.   CustomDataModule: TCustomDataModule;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. procedure TCustomDataModule.MyShow;
  28. begin
  29.   ShowMessage('OK');
  30. end;
  31.  
  32. end.
  33.  

Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Sqlite3DS, db, FileUtil,
  9.  
  10.   Unit2;
  11.  
  12. type
  13.  
  14.   { TDataModule1 }
  15.  
  16.   TDataModule1 = class(TCustomDataModule)
  17.     DataSource1: TDataSource;
  18.     Sqlite3Dataset1: TSqlite3Dataset;
  19.   end;
  20.  
  21. var
  22.   DataModule1: TCustomDataModule;
  23.  
  24. implementation
  25.  
  26. {$R *.lfm}
  27.  
  28. end.
  29.  

Blaazen

  • Hero Member
  • *****
  • Posts: 2966
  • POKE 54296,15
    • Eye-Candy Controls
Re: Abstract classes
« Reply #16 on: March 28, 2018, 08:16:37 pm »
@ As I said, I create simple demo. I can't get any compiler warnings:

Where do you create DataModule1?!
No creating, no warning.  :)

EDIT:

This gives warnings:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   DataModule1:=TDataModule1.Create(self);
  4.   DataModule1.MyShow;
  5.   DataModule1.Free;
  6. end;  
« Last Edit: March 28, 2018, 08:18:54 pm by Blaazen »
Lazarus 2.1.0 r64074 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

totya

  • Hero Member
  • *****
  • Posts: 640
Re: Abstract classes
« Reply #17 on: March 28, 2018, 08:22:36 pm »
Where do you create DataModule1?!
No creating, no warning.  :)

OMG... Button1.Click is working, because Datamodule1 is created (with autocreate), see attached file my previous post, or see this:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, sqlite3laz, Unit1, Unit2, Unit3
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource:=True;
  17.   Application.Initialize;
  18.   Application.CreateForm(TDataModule1, DataModule1);
  19.   Application.CreateForm(TForm1, Form1);
  20.   Application.Run;
  21. end.
  22.  

Blaazen

  • Hero Member
  • *****
  • Posts: 2966
  • POKE 54296,15
    • Eye-Candy Controls
Re: Abstract classes
« Reply #18 on: March 28, 2018, 08:43:37 pm »
Ah, I see now. Coincidently, it didn't crash when DataModule1 wasn't autocreated.

So yes, in this case there are no warnings. Probably because CreateForm creates instance in unusual way.
Lazarus 2.1.0 r64074 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

totya

  • Hero Member
  • *****
  • Posts: 640
Re: Abstract classes
« Reply #19 on: March 28, 2018, 09:13:59 pm »
This gives warnings:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   DataModule1:=TDataModule1.Create(self);
  4.   DataModule1.MyShow;
  5.   DataModule1.Free;
  6. end;  

Yes, me too. TDatamodule isn't a really form... even hidden... Thanks for the tip!

PascalDragon

  • Hero Member
  • *****
  • Posts: 2275
  • Compiler Developer
Re: Abstract classes
« Reply #20 on: April 06, 2018, 04:18:37 pm »
For those that want the compiler to stop when creating an abstract class or a class with abstract methods: use the following in an include file or so.  8)
Code: Pascal  [Select][+][-]
  1. {$warn 4122 error} // creating an abstract class
  2. {$warn 4046 error} // creating a class with an abstract method

Thaddy

  • Hero Member
  • *****
  • Posts: 10526
Re: Abstract classes
« Reply #21 on: April 06, 2018, 04:23:26 pm »
 :D
 8)

 

TinyPortal © 2005-2018