pointer to member without 'this' - mem_fun like

Vlad b100dian at gmail.com
Tue Mar 18 06:18:37 PDT 2008


Jarrett Billingsley wrote:
> "Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message 
> news:fro9ip$dib$1 at digitalmars.com...
>>> Thanks, this should do it - however I still need an(other) instance when 
>>> passing the delegate, right?
>> You *might* get away with using &Class.memberfn to fill in dg.funcptr. But 
>> I've never tried this, so you might want to make sure it works before 
>> using it for anything serious.
> 
> It does work, at least in my experience.  Of course this bypasses any vtable 
> lookup so you'll just be getting the implementation of the method from 
> Class. 
> 
> 

It didn't work for me (see full code below)..

On the same topic, I see in the "Template parameters" heading here 
(http://www.digitalmars.com/d/1.0/templates-revisited.html) that 
template syntax to force a member of a type does not exist either:(

I am trying to generalize the initialization of the fields of a 
struct/class with setters from something like a database rowset (and 
also to learn D;)

(here's the code: change &author.name to &Author.name)

import std.stdio;

void set_field(T,F)(T instance, void delegate(F) setter)
{
     setter("zz");
}

class Author {
     private string _name;
     string name() {
         return _name;
     }
     void name(string name) {
         _name = name;
     }
}

int main(char[][] args)
{
     Author author = new Author();
     set_field!(Author,string)(author, &author.name);
     writefln(author.name);
     return 0;
}


More information about the Digitalmars-d-learn mailing list