digitalmars.D - Call method via function pointer and object instance?
- Garett Bass (16/16) Oct 31 2005 The following function pointer assignment doesn't cause an error at comp...
- clayasaurus (4/33) Oct 31 2005 I think you want to use delegate() instead.
- BCS (30/63) Nov 01 2005 How could you do this with delegates?
- Dwight Freeney (3/10) Nov 01 2005 I like this idea. I would really like a way to retarget the delegates
- BCS (23/33) Nov 01 2005 their is the problem that this could be done:
- clayasaurus (5/49) Nov 01 2005 I'm sorry, I just realized my post wasn't too helpful. I didn't read the...
- BCS (25/41) Nov 03 2005 Check out this little gem?
- David Medlock (10/53) Nov 03 2005 I would call that a bug.
- BCS (3/50) Nov 03 2005 with a little fiddleing it seems that it is using the last accessed Foo ...
- Sean Kelly (3/44) Nov 03 2005 Why does this even work? Foo.bar() is nonstatic.
- BCS (2/46) Nov 03 2005 Beats me! It shouldn't compile and why it dosn't seg-v is puzzle.
The following function pointer assignment doesn't cause an error at compile time, but I don't know how to supply an object instance in order to call the referenced method. Note I don't want to use a delegate, because I want to call the referenced method on an object provided independently. class Foo { void bar() {writefln("bar()");} } int main(char[][] args) { void function() pBar = &Foo.bar; Foo foo = new Foo(); // syntax to call pBar with instance foo? return 0; } Thanks, Garett
Oct 31 2005
Garett Bass wrote:The following function pointer assignment doesn't cause an error at compile time, but I don't know how to supply an object instance in order to call the referenced method. Note I don't want to use a delegate, because I want to call the referenced method on an object provided independently. class Foo { void bar() {writefln("bar()");} } int main(char[][] args) { void function() pBar = &Foo.bar;I think you want to use delegate() instead. http://www.digitalmars.com/d/type.htmlFoo foo = new Foo(); // syntax to call pBar with instance foo?pBar();return 0; } Thanks, Garett
Oct 31 2005
How could you do this with delegates? Or even better have fn be an arg to a function. class class Foo { void bar() {writefln("bar()\n");} void stick() {writefln("stick()\n");} } int main(char[][] argv) { Foo[10] f; void function() fn; if(argv.length == 1) fn = &Foo.bar; else fn = &Foo.stick; foreach(Foo fi; f) { // syntax to call fn with instance fi? } return 0; } if delegates had a "this" property then you could do this: void delegate() fn; .. foreach(Foo fi; f) { fn.this = fi; fn(); } In article <dk6p47$28nh$1 digitaldaemon.com>, clayasaurus says...Garett Bass wrote:The following function pointer assignment doesn't cause an error at compile time, but I don't know how to supply an object instance in order to call the referenced method. Note I don't want to use a delegate, because I want to call the referenced method on an object provided independently. class Foo { void bar() {writefln("bar()");} } int main(char[][] args) { void function() pBar = &Foo.bar;I think you want to use delegate() instead. http://www.digitalmars.com/d/type.htmlFoo foo = new Foo(); // syntax to call pBar with instance foo?pBar();return 0; } Thanks, Garett
Nov 01 2005
I like this idea. I would really like a way to retarget the delegates object without resetting the method offset. BCS wrote:foreach(Foo fi; f) { fn.this = fi; fn(); }
Nov 01 2005
their is the problem that this could be done: class Foo { void bar() { writef(r, \n); } int i; } int main() { void bar2() { r++; } real r; void delagate() fn = bar2; Foo f; fn.this = f; // what type is fn.this ?? fn(); // what happens??? } some sort of runtime and/or compile time check is needed. In article <dk895u$gmc$1 digitaldaemon.com>, Dwight Freeney says...I like this idea. I would really like a way to retarget the delegates object without resetting the method offset. BCS wrote:foreach(Foo fi; f) { fn.this = fi; fn(); }
Nov 01 2005
I'm sorry, I just realized my post wasn't too helpful. I didn't read the 'I don't want to use a delegate' part. I don't have any useful suggestions because I don't understand what you are trying to do. ~ Clay clayasaurus wrote:Garett Bass wrote:The following function pointer assignment doesn't cause an error at compile time, but I don't know how to supply an object instance in order to call the referenced method. Note I don't want to use a delegate, because I want to call the referenced method on an object provided independently. class Foo { void bar() {writefln("bar()");} } int main(char[][] args) { void function() pBar = &Foo.bar;I think you want to use delegate() instead. http://www.digitalmars.com/d/type.htmlFoo foo = new Foo(); // syntax to call pBar with instance foo?pBar();return 0; } Thanks, Garett
Nov 01 2005
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? In article <dk6non$2840$1 digitaldaemon.com>, Garett Bass says...The following function pointer assignment doesn't cause an error at compile time, but I don't know how to supply an object instance in order to call the referenced method. Note I don't want to use a delegate, because I want to call the referenced method on an object provided independently. class Foo { void bar() {writefln("bar()");} } int main(char[][] args) { void function() pBar = &Foo.bar; Foo foo = new Foo(); // syntax to call pBar with instance foo? return 0; } Thanks, Garett
Nov 03 2005
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?I would call that a bug. You cannot(afaik) access a non-static function in a class using a function pointer, it must be a delegate. It must be: void delegate() pBar = &foo1.bar; or make bar a static function. For some reason it sets the pointer to the first created instance of Foo.bar in the current implementation. -DavidM
Nov 03 2005
In article <dkdqpn$b4s$1 digitaldaemon.com>, David Medlock 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?For some reason it sets the pointer to the first created instance of Foo.bar in the current implementation. -DavidMwith a little fiddleing it seems that it is using the last accessed Foo object.
Nov 03 2005
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
Nov 03 2005
In article <dkdurc$g95$2 digitaldaemon.com>, Sean Kelly says...BCS wrote:Beats me! It shouldn't compile and why it dosn't seg-v is puzzle.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
Nov 03 2005