RTTI and detecting virtual method overrides

RTTI and detecting virtual method overrides

What is the best way of programmatically detecting at runtime whether a virtual method in the base class has been overridden for a descendant type?

Calling the method is not an option, as an instance does not exist as of yet, and I only have a type reference.

15 thoughts on “RTTI and detecting virtual method overrides

  1. Are you checking the constructor? If not you can create the instance then check, or create a set of “blank” instances which you’ll use for the purpose of checking.

    Once you have instances, you can just compare the address of the method, ie. if base.Method@sub.Method then it’s overridden.

    This is fast, foolproof, can also work for private methods without involving RTTI (just perform the check in a method of the base class), and it provides a degree of compiler support.

  2. Stefan Glienke – Correct.

    Eric Grange – The base class is an abstract class, so not instantiable. The methods that I need to check are not virtual abstract, but the base class implementation simply tosses an exception if they happen to be called in the default implementation.  They are also declared in the protected section.

    I guess the easiest way around is to add a public function in the base class that does the call and checks for an exception of the expected type.

  3. Nicholas Ring Not this article but probably another one from Hallvard might help. That one is about the extended class rtti (when you set $M+) and provides information about arguments.

  4. I already posted the solution I think.

    P.S.: I think there is still one little bug with the skipping of the TVmtMethodEntry array but that only matters for a class with published methods. I will fix that later.