Respond to an unknown method call: Difference between revisions

Content added Content deleted
Line 493: Line 493:
An exception about calling a virtual method will be raised and interrupt the current code flow. But in that situation, the function is actually known. The concept of a function not being known at all does not exist in Object Pascal, due to static name lookup checking. That is to say, the call a.b() causes the compiler to verify that the symbol b exists in the namespace associated with expression a, and refers to a function.
An exception about calling a virtual method will be raised and interrupt the current code flow. But in that situation, the function is actually known. The concept of a function not being known at all does not exist in Object Pascal, due to static name lookup checking. That is to say, the call a.b() causes the compiler to verify that the symbol b exists in the namespace associated with expression a, and refers to a function.


To avoid the possibility of an abstract method call, one common solution is to leave an empty implementation for the virtual method instead of making it abstract, like this:
To avoid the possibility of an abstract method call, one common solution is to leave an empty implementation for the virtual method instead of making it abstract. This is what an abstract method call looks like:


<lang pascal>
<lang pascal>
Line 499: Line 499:
Tanimal = class
Tanimal = class
public
public
procedure bark(); virtual; // virtual, but not abstract
procedure bark(); virtual; abstract;
end;
end;


implementation
implementation

procedure Tanimal.bark();
begin
end;


var
var
Line 514: Line 510:


animal := Tanimal.Create;
animal := Tanimal.Create;
animal.bark();
animal.bark(); // abstract method call exception at runtime here
animal.Free;
animal.Free;