【发布时间】:2011-05-09 18:42:52
【问题描述】:
我想用 c++(或 c++0x)编写一个指针,它将指向一个类的运算符,比如说 A 或 B。 有什么方法可以做到吗?
当然有像
这样的语法int (A::*_p) ();
但它并没有解决这个问题。我想制作通用指针,而不是为其指定基类 - 仅用于“操作符函数”的指针
#include <thread>
#include <iostream>
using namespace std;
class A
{
public:
int operator()()
{
return 10;
}
};
class B
{
public:
int operator()()
{
return 11;
}
};
int main()
{
A a;
int (*_p) ();
_p = a.operator();
cout << _p();
B b;
_p = b.operator();
cout << _p();
}
【问题讨论】:
标签: c++ pointers c++11 function-pointers operator-keyword