[fpc-pascal] Assigning a virtual class method to a method pointer
Joao Morais
post at joaomorais.com.br
Sun Feb 25 02:41:54 CET 2007
Hello,
I need to check if a virtual class method was implemented in an specific
subclass. I am assigning two methods to two method pointers, casting to
tmethod and comparing its code fields. The application raises a
segmentation fault in this line:
vmethodptr2 := @tbaseclassref(classparent).classmethod;
The following code raises the exception using both 2.0.4 and 2.1.1.
====
{$mode objfpc}
program testclass;
type
tbaseclassref = class of tbaseclass;
tbaseclass = class(tobject)
public
class function classmethod: string; virtual;
class function implementsclassmethod: boolean;
end;
tinheritedyesclass = class(tbaseclass)
public
class function classmethod: string; override;
end;
tinheritednoclass = class(tbaseclass)
end;
class function tbaseclass.implementsclassmethod: boolean;
var
vmethodptr1, vmethodptr2: function: string of object;
begin
if self <> tbaseclass then
begin
vmethodptr1 := @classmethod;
// here is the problem
vmethodptr2 := @tbaseclassref(classparent).classmethod;
result := tmethod(vmethodptr1).code <> tmethod(vmethodptr2).code;
end else
result := false;
end;
class function tbaseclass.classmethod: string;
begin
result := '';
end;
class function tinheritedyesclass.classmethod: string;
begin
result := 'some stuff';
end;
const
cboolvalues: array[boolean] of string = ('no', 'yes');
begin
writeln(cboolvalues[tinheritedyesclass.implementsclassmethod]);
writeln(cboolvalues[tinheritednoclass.implementsclassmethod]);
end.
====
Am I doing something wrong or this is a bug?
Thanks,
--
Joao Morais
More information about the fpc-pascal
mailing list