Pointer to method C++ style

Sergey Gromov snake.scaly at gmail.com
Wed Jul 22 20:47:30 PDT 2009


Is there a way to declare and statically initialize some sort of pointer
to method, and later call it for an actual object instance?

Use case: I'm writing an ActiveX plug-in for a dynamic language.  The
language queries the component for implemented methods, then it requests
these methods to be executed.  I want to implement these methods as the
component class non-static members, and create a lookup table at compile
time, sort of

struct Method
{
    string name;
    PointerToMember method;  // some magic goes here
}

class Component : IUnknown
{
    void method1() {}
    void method2() {}

    static Method[] LOOKUP_TABLE =
    [
        { "method1", &method1 },  // won't work, &method1 is non-const
        { "method2", &method2 }
    ];
    
    void call(int i)
    {
        this->*LOOKUP_TABLE[i].method(); // sort of
    }
}


More information about the Digitalmars-d-learn mailing list