I tried this, and some variations on it:
template<class T>
class Ptr {
public:
Ptr(T* ptr) : p(ptr) {}
~Ptr() { if(p) delete p; }
template<class Method>
Method operator ->* (Method method)
{
return p->*method;
}
private:
T *p;
};
class Foo {
public:
void foo(int) {}
int bar() { return 3; }
};
int main() {
Ptr<Foo> p(new Foo());
void (Foo::*method)(int) = &Foo::foo;
int (Foo::*method2)() = &Foo::bar;
(p->*method)(5);
(p->*method2)();
return 0;
}
But it doesn't work. The problem is that I don't really know what to expect as a parameter or what to return. The standard on this is incomprehensible to me, and since Google did not bring up anything helpful I guess I'm not alone.
Edit: Another try, with C++0x: http://ideone.com/lMlyB
See Question&Answers more detail:os