The return of operator->*
represents a function in the process of being called, with the only missing parts being the parameters. Thus, you must return a functor that invokes the given function on the given object with the given parameters:
// PTMF = pointer to member function
template<class Obj>
struct PTMF_Object{
typedef int (Obj::*ptmf)(double,std::string); // example signature
PTMF_Object(Obj* obj, ptmf func)
: obj_(obj)
, func_(func)
{}
int operator()(double d, std::string str){
return (obj_->*func_)(d,str);
}
Obj* obj_;
ptmf func_;
};
template<class T>
struct SmartPtr{
// ...
PTMF_Object<T> operator->*(PTMF_Object<T>::ptmf func){
return PTMF_Object<T>(p, func);
}
// ...
};
int main(){
SmartPtr<Foo> pf(new Foo());
typedef int (Foo::*Foo_ptmf)(double,std::string);
Foo_ptmf method = &Foo::bar;
(pf->*method)(5.4, "oh hi");
}
Edit2
Here is an excellent pdf from Scott Meyers on this very topic (it's the only good literature on overloading operator->*
, even though it's from 1999).
Edit
Here is one, if your compiler supports variadic templates: http://ideone.com/B6kRF
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…