先看一个例子:
//统一的接口
include <iostream>
using namespace std;
struct Point{
int plus(double x, double y){ return x + y; } int minus(double x, double y){ return x - y; } int mul(double x, double y){ return x*y; } int div(double x, double y){ return x/y; }
};
int oper(Point & point, int (Point::*pfun)(double x, double y),
double x, double y){ return (point.*pfun)(x, y);
}
int main()
{
Point pt; int (Point::*pfun)(double, double); pfun = &Point::plus; cout<<oper(pt, pfun, 10, 20); pfun = &Point::minus; cout<<oper(pt, pfun, 10, 20); return 0;
}
再看一个例子:
//更加隐蔽的接口
include <iostream>
using namespace std;
class Widget{
public:
Widget(){ pf[0] = &Widget::f; pf[1] = &Widget::g; pf[2] = &W