D1/D2: How to check if a method has been overridden

dsimcha dsimcha at yahoo.com
Tue Sep 7 14:12:01 PDT 2010


One way to test for overriding at runtime is to compare the function pointer of a
delegate obtained at runtime to the function pointer obtained from the compile
time type.

Here's a simplified example:

import std.stdio;

class A {
    void fun() {}
}

class B : A {
    override void fun() {}
}

void main() {
    auto a = new A;
    auto b = new B;

    auto aDel = &a.fun;
    auto bDel = &b.fun;

    writeln(&A.fun is aDel.funcptr);  // True:  fun is not overridden
    writeln(&A.fun is bDel.funcptr);  // False:  fun is overridden
}


More information about the Digitalmars-d mailing list