Respond to an unknown method call: Difference between revisions

Content added Content deleted
Line 487: Line 487:
object.print("Hi") -->Hi
object.print("Hi") -->Hi
object.hello() -->You called the method hello
object.hello() -->You called the method hello
</lang>

=={{header|C++}}==

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:

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

implementation

procedure Tanimal.bark();
begin
end;

var
animal: Tanimal;

initialization

animal := Tanimal.Create;
animal.bark();
animal.Free;

end.
</lang>
</lang>