以以下为例:(注意,该示例不起作用,但应该足以说明我要做什么)
class Point {
float x, y;
public:
float getX() const { return x; }
float getY() const { return y; }
};
class Polygon {
std::vector<Point> points;
std::vector<float> get(float (Point::*func)()const) {
std::vector<float> ret;
for(std::vector<Point>::iterator it = points.begin(); it != points.end(); it++) {
// call the passed function on the actual instance
ret.push_back(it->*func());
}
return ret;
}
public:
std::vector<float> getAllX() const {
return get(&Point::getX); // <- what to pass for getX
}
std::vector<float> getAllY() const {
return get(&Point::getY); // <- what to pass for getY
}
};
编辑:
问题是操作顺序;编译器需要在调用周围加上括号,如下所示:
(it->*func)()