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.

Comments

  1. So you have 2 class refs and want to check if a particular method has been overridden by the 2nd class?

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. I don't know if this is any use: http://hallvards.blogspot.com.au/2006/09/extended-class-rtti.html

    Since you have type reference, maybe you could iterate the methods in the VMT in both classes (references) and see if the addresses are the same...? A bit messy but it might work (or not) ;-)

    ReplyDelete
  5. 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.

    ReplyDelete
  6. Stefan Glienke  I choose that one because of the source but I will not say that your are wrong - just hopefully pointing people in the right direction :-)

    ReplyDelete
  7. This probably covers what you are looking for

    http://hallvards.blogspot.com/2006/03/hack-8-explicit-vmt-calls.html?m=1

    You can find each method in the vmt table then check if the methods of two classes (where one inherits from the other) have the same vmt index.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. Indeed you did, Stefan Glienke :)

    I'm on a phone so I didn't follow all the links.

    ReplyDelete
  10. Hallvard! Great to see you. I hope all is well.

    ReplyDelete
  11. I ended up with virtual methods in the base class instead.  Less magic.

    ReplyDelete
  12. Hallvard Vassbotn Could not agree more! ;)

    ReplyDelete
  13. Hallvard Vassbotn Stefan Glienke Could not agree more and it is even more fun when you understand some-one else's magic.

    ReplyDelete

Post a Comment