Lazarus

Programming => General => Topic started by: coucout on August 11, 2021, 03:24:30 pm

Title: POO : no compilation error for abstract virtual method not implemented
Post by: coucout on August 11, 2021, 03:24:30 pm
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.
Title: Re: POO : no compilation error for abstract virtual method not implemented
Post by: MarkMLl 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
Title: Re: POO : no compilation error for abstract virtual method not implemented
Post by: lucamar 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.
Title: Re: POO : no compilation error for abstract virtual method not implemented
Post by: PascalDragon 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