[fpc-pascal]Bug with virtual methods

Peter Vreman peter at freepascal.org
Tue Dec 31 12:14:59 CET 2002

> Hi!
> Please have a look at the attached program. There are two classes, the
> second one inherits all methods from the first one.
> The method M1 with the string parameter is the "acting" function. That
> one with the Integer parameter is just a wrapper to handle integers.
> In the second class the string method is overridden. Therefore it was
> defined virtual in C1. C1.M1(I:Integer), which is inherited to C2,
> always calls the string method which belongs to the class.
> The main program demonstrates this. It creates two instances, one of C1,
> one of C2. Then C1.M1(String) is executet, its wrapper C1.M1(Integer).
> Both work properly.
> Then C2.M1(String) is executed. Finally C2.M1(Integer) is executed. Here
> a compiler error occurs. The compiler has forgotten to inherit the
> C1.M1(Integer) to C2!!!
> If C1.M1(Integer) is renamed to C1.M2(Integer) no problem occurs. It
> seems, the compiler only inherits _one_ of parameter-overloaded methods.

That is correct behaviour of the compiler. The compiler only searches in
the scope of C2 for M1. It is the same as with 2 units. U1 and U2 instead
of C1 and C2.

In the 1.1 compiler you can explicitly specify that you want to overload
the function M1 by adding the 'overload' directive (works also for cross
unit overloading). When this is added to the definition of C2.M1 the
program compiles.

More information about the fpc-pascal mailing list