c++ pointers to operators

空扰寡人 提交于 2019-12-03 11:29:57

No, you can't do this. The class type is a part of the type of the operator member function.

The type of A::operator()() is different from the type of B::operator()(). The former is of type int (A::*)() while the latter is of type int (B::*)(). Those types are entirely unrelated.

The closest you can get is by using something like the C++0x polymorphic function wrapper function (found in C++0x, C++ TR1, and Boost) and by using bind to bind the member function pointer to a class instance:

std::function<int()> _p;

A a;
_p = std::bind(&A::operator(), a);
std::cout << _p();

B b;
_p = std::bind(&B::operator(), b);
std::cout << _p();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!