digitalmars.D.learn - Address of instance member function
- Max Samukha (13/13) Mar 31 2007 What is the meaning of the value returned by the address operator
- Jarrett Billingsley (25/38) Mar 31 2007 It's a function pointer to that method. It's actually useful -- you can...
- Max Samukha (3/43) Mar 31 2007 Thanks a lot. That's exactly what I need
- Deewiant (5/34) Mar 31 2007 Are there other uses? Doesn't the above essentially boil down to just:
- Chris Nicholson-Sauls (5/37) Mar 30 2007 It does, except that you could later set dg.ptr to a /different/ instanc...
- Kirk McDonald (21/65) Mar 31 2007 It doesn't, really.
What is the meaning of the value returned by the address operator applied to an instance member function as if that function were static? class Foo { void bar() { } } void main() { writefln(&Foo.bar); }
Mar 31 2007
"Max Samukha" <samukha voliacable.com> wrote in message news:11js039sich4vjo364at18so5m625bdvc8 4ax.com...What is the meaning of the value returned by the address operator applied to an instance member function as if that function were static? class Foo { void bar() { } } void main() { writefln(&Foo.bar); }It's a function pointer to that method. It's actually useful -- you can simulate pointer-to-members using this and delegates: class A { int mX; this(int x) { mX = x; } void foo(int y) { writefln(mX, ", ", y); } } void main() { scope a = new A(5); a.foo(4); void delegate(int) dg; dg.funcptr = &A.foo; // <<- dah dg.ptr = cast(void*)a; dg(8); }
Mar 31 2007
On Sat, 31 Mar 2007 11:43:47 -0400, "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote:"Max Samukha" <samukha voliacable.com> wrote in message news:11js039sich4vjo364at18so5m625bdvc8 4ax.com...Thanks a lot. That's exactly what I needWhat is the meaning of the value returned by the address operator applied to an instance member function as if that function were static? class Foo { void bar() { } } void main() { writefln(&Foo.bar); }It's a function pointer to that method. It's actually useful -- you can simulate pointer-to-members using this and delegates: class A { int mX; this(int x) { mX = x; } void foo(int y) { writefln(mX, ", ", y); } } void main() { scope a = new A(5); a.foo(4); void delegate(int) dg; dg.funcptr = &A.foo; // <<- dah dg.ptr = cast(void*)a; dg(8); }
Mar 31 2007
Jarrett Billingsley wrote:It's a function pointer to that method. It's actually useful -- you can simulate pointer-to-members using this and delegates: class A { int mX; this(int x) { mX = x; } void foo(int y) { writefln(mX, ", ", y); } } void main() { scope a = new A(5); a.foo(4); void delegate(int) dg; dg.funcptr = &A.foo; // <<- dah dg.ptr = cast(void*)a;Are there other uses? Doesn't the above essentially boil down to just: auto dg = &a.foo;dg(8); }-- Remove ".doesnotlike.spam" from the mail address.
Mar 31 2007
Deewiant wrote:Jarrett Billingsley wrote: > It's a function pointer to that method. It's actually useful -- you canIt does, except that you could later set dg.ptr to a /different/ instance. Its utility is in late binding to instances selected by some arbitrary (and possibly external) means. I'd like to see if it plays well with inheritance, though. -- Chris Nicholson-Saulssimulate pointer-to-members using this and delegates: class A { int mX; this(int x) { mX = x; } void foo(int y) { writefln(mX, ", ", y); } } void main() { scope a = new A(5); a.foo(4); void delegate(int) dg; dg.funcptr = &A.foo; // <<- dah dg.ptr = cast(void*)a;Are there other uses? Doesn't the above essentially boil down to just: auto dg = &a.foo;
Mar 30 2007
Chris Nicholson-Sauls wrote:Deewiant wrote:It doesn't, really. class Base { void foo() { writefln("Base"); } } class Derived : Base { void foo() { writefln("Derived"); } } void main() { void delegate() dg; dg.funcptr = &Base.foo; dg.ptr = new Derived; dg(); // will print "Base" } However, this is precisely the behavior I would expect, and in fact Pyd relies on it. -- Kirk McDonald http://kirkmcdonald.blogspot.com Pyd: Connecting D and Python http://pyd.dsource.orgJarrett Billingsley wrote: > It's a function pointer to that method. It's actually useful -- you canIt does, except that you could later set dg.ptr to a /different/ instance. Its utility is in late binding to instances selected by some arbitrary (and possibly external) means. I'd like to see if it plays well with inheritance, though. -- Chris Nicholson-Saulssimulate pointer-to-members using this and delegates: class A { int mX; this(int x) { mX = x; } void foo(int y) { writefln(mX, ", ", y); } } void main() { scope a = new A(5); a.foo(4); void delegate(int) dg; dg.funcptr = &A.foo; // <<- dah dg.ptr = cast(void*)a;Are there other uses? Doesn't the above essentially boil down to just: auto dg = &a.foo;
Mar 31 2007