Recent

Author Topic: POO : no compilation error for abstract virtual method not implemented  (Read 2115 times)

coucout

  • Newbie
  • Posts: 3
Hello,
Just a question : is it normal that, if I declare an abstract virtual method and I do not implement this method in subclass, the compiler do not give an error ?
Exemple, no error in this case (procedure "one" is not implemented in TDesc class.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. type
  4.   TFirstClass = class
  5.     procedure one; virtual; abstract;
  6.   end;
  7.  
  8.   TDesc = class(TFirstClass)
  9.  
  10.   end;
  11.  
  12.  
  13. begin
  14.  
  15. end.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: POO : no compilation error for abstract virtual method not implemented
« Reply #1 on: August 11, 2021, 03:53:05 pm »

$ fpc xyzzy.pas
Free Pascal Compiler version 3.2.0 [2020/11/20] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling xyzzy.pas
xyzzy.pas(4,21) Error: Identifier not found "class"
xyzzy.pas(5,9) Error: Error in type definition
xyzzy.pas(5,9) Fatal: Syntax error, ";" expected but "PROCEDURE" found
Fatal: Compilation aborted
Error: /usr/local/bin/ppcx64 returned an error exitcode


Just how many more errors do you want: >:-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: POO : no compilation error for abstract virtual method not implemented
« Reply #2 on: August 11, 2021, 04:02:36 pm »
@Mark: Yeah, those happen because the default mode (fpc) doesn't allow classes; rather try this:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc} {or: $modeswith class}
  4.  
  5. type
  6.   TFirstClass = class
  7.     procedure one; virtual; abstract;
  8.   end;
  9.  
  10.   TDesc = class(TFirstClass)
  11.   end;
  12.  
  13. begin
  14. end.

The result then is:
Code: Text  [Select][+][-]
  1. lucamar@Diana:~/SoftDev/zTemp/test$ fpc test.pas
  2. Free Pascal Compiler version 3.2.0 [2020/07/07] for x86_64
  3. Copyright (c) 1993-2020 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling test.pas
  6. Linking test
  7. 15 lines compiled, 0.1 sec

Just a question : is it normal that, if I declare an abstract virtual method and I do not implement this method in subclass, the compiler do not give an error ?

Yes, it's normal. It's up to the child to override an abstract (or virtual) method but it doesn't have to do it if it's not needed in the child. If you then try to access that method through a child class instance the parent's method is called and, since it's abstract, will raise a "call to abstract method" exception.
« Last Edit: August 11, 2021, 04:04:39 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: POO : no compilation error for abstract virtual method not implemented
« Reply #3 on: August 12, 2021, 08:55:11 am »
Just a question : is it normal that, if I declare an abstract virtual method and I do not implement this method in subclass, the compiler do not give an error ?

The compiler will only generate such warnings (they are not errors by default) if you instantiate the class. So add a call to TDesc.Create and you'll get a warning.

 

TinyPortal © 2005-2018