digitalmars.D - D1: Member function delegate issues
- klickverbot (71/71) Nov 25 2009 (This would probably fit better on bugzilla, but for whatever reason I
(This would probably fit better on bugzilla, but for whatever reason I cannot access it at the moment. Please feel free to move this to the tracker and CC me if it is really a bug.) As you might remember from #d and d.D.learn, I want to check if a given member function has been overridden (at runtime, of course). A simple test using delegates looks promising: --- class A { public: void foo() { Stdout( "I am in A." ).newline; } } class B : A { public: override void foo() { Stdout( "I am in B." ).newline; } } void main() { auto baseMethod = &A.foo; A a = new A(); a.foo(); auto aMethod = &a.foo; Stdout.formatln( "foo overridden in a: {}", ( baseMethod !is aMethod.funcptr ) ); A b = new B(); b.foo(); auto bMethod = &b.foo; Stdout.formatln( "foo overridden in b: {}", ( baseMethod !is bMethod.funcptr ) ); } --- For use in my application, I tried to move the checking code to A, which should be no problem, at least according to the principle of least surprise: --- class A { public: void foo() { Stdout( "I am in A." ).newline; } bool isFooOverridden() { auto methodThis = &foo; auto methodBase = &A.foo; return ( methodThis.funcptr !is methodBase.funcptr ); } } class B : A { public: override void foo() { Stdout( "I am in B." ).newline; } } void main() { A a = new A(); a.foo(); Stdout.formatln( "foo overridden in a: {}", a.isFooOverridden() ); A b = new B(); b.foo(); Stdout.formatln( "foo overridden in b: {}", b.isFooOverridden() ); } --- However, this does not work because the address of A.foo is looked up at runtime via the vtable in this case, effectively defeating the intent behind the pointer comparison. Have I hit a bug here or is it written somewhere in the spec? If this should really be the intended behavior, I would strongly suggest altering it because it seems highly counterintuitive to me. Moveover, I cannot really see that the current behavior would have any benefits. Sorry if this has already been discussed, but as I mentioned before, I cannot access the D bugzilla at the moment…
Nov 25 2009