member pointers

Daniel Keep daniel.keep.lists at gmail.com
Mon Mar 12 18:44:15 PDT 2007


bc wrote:
> 
> am i being stupid or is there any chance of adding pointers to member
> functions and pointers to data members to D? i can't see any way of
> getting the same functionality through existing language constructs for
> member functions... alias seems like it might at first but i can't get
> it to work... and the offsetof syntax for data members seems buggy.
> otherwise, congrats on designing a really cool language

Let's see...

> import std.stdio;
>
> class Foo
> {
>     uint xzzxy = 0;
>
>     void bar()
>     {
>         writefln("Bar, bar, member func, have you any Foo?");
>         writefln("The magic number is: %d", this.xzzxy);
>     }
> }
>
> alias void delegate() voiddg; // <-- just for shorthand
>
> void main()
> {
>     Foo baz = new Foo;
>
>     uint* quxxy = &baz.xzzxy; // <-- pointer to an attribute
>     *quxxy = 42;
>
>     voiddg dg = &baz.bar; // <-- "pointer" to our member function
>     dg();
> }

Delegates are your pointer to member functions.  Pointers to data of an
object are just as you would expect.  Or did you have a different use in
mind?

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list