[fpc-pascal] How to know if a class implements a method

Joao Morais post at joaomorais.com.br
Fri May 18 16:24:47 CEST 2007

Hello Matt, thanks for your fast input.

Matt Emson wrote:

>> Any idea how I know if a class, in a class pointer, overrides a virtual
>> method? Eg:
> <snip>
>> vfooclass := tboo1;
>> // vfooclass doesn't implement sample.
>> vfooclass := tboo2;
>> // vfooclass implements sample.
> You need to implement a virtual method, even if it does nothing. Are you
> sure you're not meaning abstract method?

No, no... I just want to know if the class referenced by vfooclass 
overrides the "sample" method, implementing something different from the 
virtual implementation that, btw, isn't abstract and it's empty.

>> At this moment I know that:
>> 1. I could cast a method pointer with tmethod if sample was a class
> method;
>> 2. I could cast this same method pointer if I had an instance of
> vfooclass.
>> But is there another way beyond creating an instance?
> You can always hack the RTTI/VMT, but that might not be a safe way.

I though about this, dunno it's better than creating an useless instance.

> Might it be that you need to have a more complex inheritence tree? So add a
> few levels?

Even more complex? No, no! =)

This is a piece of an OPF framework. I want to know if a business class 
implement something in a virtual method. Such class belongs to the 
application and the framework doesn't have direct access to it. If the 
class doesn't implement I don't need to retrieve an instance from the 
database, btw an expensive task for absolutely nothing.

So, at this moment I know that I can:

1. Use a safe, magic and efficient way, but I need to know if it exists;
2. Create a "light" instance using vfooclass.NewInstance method;
3. hack the vmt.

Which approach do you use?


Joao Morais

More information about the fpc-pascal mailing list