Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 01, 2005 Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
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 |
November 01, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | 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.html > > Foo foo = new Foo(); > > // syntax to call pBar with instance foo? pBar(); > > return 0; > } > > Thanks, > Garett > > > > > > |
November 01, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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.html > >> >> Foo foo = new Foo(); >> >> // syntax to call pBar with instance foo? > > pBar(); > >> >> return 0; >> } >> >> Thanks, >> Garett >> >> >> >> >> >> |
November 01, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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();
> }
>
|
November 01, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dwight Freeney | 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(); >> } >> |
November 01, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to clayasaurus | 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.html
>
>>
>> Foo foo = new Foo();
>>
>> // syntax to call pBar with instance foo?
>
>
> pBar();
>
>>
>> return 0;
>> }
>>
>> Thanks,
>> Garett
>>
>>
>>
>>
>>
>>
|
November 03, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Garett Bass | 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 > > > > > > |
November 03, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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
|
November 03, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | 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. > >-DavidM with a little fiddleing it seems that it is using the last accessed Foo object. |
November 03, 2005 Re: Call method via function pointer and object instance? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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
|
Copyright © 1999-2021 by the D Language Foundation