www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Call method via function pointer and object instance?

reply "Garett Bass" <garettbass studiotekne.com> writes:
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
next sibling parent reply clayasaurus <clayasaurus gmail.com> writes:
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
 
 
 
 
 
 
Oct 31 2005
next sibling parent reply BCS <BCS_member pathlink.com> writes:
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
 
 
 
 
 
 
Nov 01 2005
parent reply Dwight Freeney <dt apt.com> writes:
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
parent BCS <BCS_member pathlink.com> writes:
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
prev sibling parent clayasaurus <clayasaurus gmail.com> writes:
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
Nov 01 2005
prev sibling parent reply BCS <BCS_member pathlink.com> writes:
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
next sibling parent reply David Medlock <noone nowhere.com> writes:
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
parent BCS <BCS_member pathlink.com> writes:
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.
Nov 03 2005
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
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
parent BCS <BCS_member pathlink.com> writes:
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.
Nov 03 2005