pointers-to-members, (custom) array implementation

BCS BCS at pathlink.com
Wed Feb 7 18:59:46 PST 2007


Wolfgang Draxinger wrote:
> I'm currently in a discussion D vs. C++ and in some points I must
> admit, that the other side has some point, I must admit, too:
> 
> Pointers to members are a feature that D currently lacks, but
> sometimes they are quite usefull and can't be replaced by
> delegates.
> 
> E.g. say you got a class "foo" which has several member functions
> of the same prototype and several member variables. Now you want
> to write some function that performs some action on the class,
> but is not fixed on certain members. That function would accept
> the instance of a class, and pointers-to-members as parameters.
> The whole thing could be used in a foreach loop.
> 
> For the member variables one could use the .offset property to
> supply their offset address relative to the class instance
> address, but for member functions emulating such functionality
> would be quite a hack. I think that pointers-to-members are as
> important as delegates.
> 

I'd have to check but I think this works

class C
{
	int one(int i){...}
	int two(int i){...}
	int three(int i){...}
}

int Pt2Mem!(alias go)(C c, int i)
{
	return c.go(i);
}


auto fn = &Pt2Mem!(one);

C c = new C;

c.fn(1);

The tail recursion should get optimized away and maybe even the first 
call in some cases.




More information about the Digitalmars-d mailing list