D1/D2: How to check if a method has been overridden
Max Samukha
spambox at d-coding.com
Wed Sep 8 02:45:52 PDT 2010
On 09/08/2010 12:33 AM, klickverbot wrote:
>
> Well, I guess I should have wrote the following instead:
>
You can avoid messing with vtable:
class A {
void foo( float a ) {}
void foo( int a ) {}
// hack to get the static address of an overload
private static Fn funcAddr(Fn, alias fn)() {
return cast(Fn)&fn;
}
private bool isOverriden(Dg, alias fn)() {
Dg dg = &foo;
// check if the static and dynamic addresses match
return dg.funcptr != funcAddr!(typeof(dg.funcptr), fn);
}
final void bar() {
writefln("%:", this.classinfo);
if (isOverriden!(void delegate(float), foo))
writefln(" foo(float) is overriden");
if (isOverriden!(void delegate(int), foo))
writefln(" foo(int) is overriden");
}
}
class B : A {
alias A.foo foo;
override void foo( float a ) {}
}
class C : A {
alias A.foo foo;
override void foo( int a ) {}
}
class D : B {
alias B.foo foo;
override void foo( int a ) {}
}
void main()
{
auto a = new A;
a.bar();
a = new B;
a.bar();
a = new C;
a.bar();
a = new D;
a.bar();
}
No guarantees at all as you never know whether you are dealing with a
bug or feature.
More information about the Digitalmars-d
mailing list