November 03, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | In article <dkdurc$g95$2@digitaldaemon.com>, Sean Kelly says... > >BCS wrote: >> Check out this little gem? >> >> import std.stdio; >> >> class Foo { >> this(int j){i=j;} >> void bar() {writef("bar()", i, \n);} >> int i; >> } >> >> int main(char[][] args) >> { >> >> void function() pBar = &Foo.bar; >> >> >> Foo foo1 = new Foo(1); // remove these and it seg-v >> Foo foo3 = new Foo(3); >> Foo foo2 = new Foo(2); >> >> >> pBar(); >> >> foo3.bar(); >> >> pBar(); >> >> return 0; >> } >> >> output: >> >> >> bar()2 >> bar()3 >> bar()3 >> >> >> >> The bug is in the function pointer assignment. The type should not be "void >> function()" it should be "void function(Foo)". Is this a bug, a feature or what? > >Why does this even work? Foo.bar() is nonstatic. > > >Sean Beats me! It shouldn't compile and why it dosn't seg-v is puzzle. |
Copyright © 1999-2021 by the D Language Foundation