如何重载 ->* 运算符?
How to overload the ->* operator?
我尝试了这个,以及它的一些变化:
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;
}
但是它不起作用。问题是,我真的不知道期望作为参数的是什么,或者返回什么。这方面的标准对我来说是不可理解的,既然谷歌没有提出任何有用的东西,我想我不是一个人。
编辑:另一个尝试,与c++ 0 x: http://ideone.com/lMlyB
operator->*
的返回表示一个函数在被调用过程中,只缺少参数部分。因此,必须返回一个函子,该函子使用给定的形参对给定对象调用给定的函数:
// 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
这是Scott Meyers关于这个话题的优秀pdf(这是关于超载operator->*
的唯一好的文献,尽管它来自1999年)。
编辑
这里有一个,如果你的编译器支持可变模板:http://ideone.com/B6kRF
我回到这个问题,我发现了一个简单的解决方案:
template<class Ret, class... Args>
auto operator ->* (Ret (T::*method)(Args...)) -> std::function<Ret(Args...)>
{
return [this, method](Args&&... args) -> Ret {
return (this->p->*method)(std::forward<Args>(args)...);
};
}
完整测试用例的实现可以在这里找到。
在这个解决方案中我唯一不喜欢的是std::function
的使用。如果有人知道直接返回lambda的方法,请告诉我。我有一种预感,由于lambda类型的定义方式,这可能是不可能的。
在IIRC中,operator->*
的返回类型应该是一个可以作为函数调用的对象。
因此,您可能不得不将指向成员函数调用的指针转发给重载operator()
(闭包)的对象,如果您手边没有可变模板,这尤其令人痛苦。
如果你有c++ 0X,你有闭包和可变模板,你的问题可以用几行泛型代码解决。
如果你不这样做,你将不得不使用宏技巧来模拟可变模板,这是不是有趣的(即使有些人认为相反)。
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 是否需要使用 - &gt;运算符在C 中调用成员函数时