1

以以下为例:(注意,该示例不起作用,但应该足以说明我要做什么)

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)()
4

2 回答 2

2

看起来您想使用“指向成员函数的指针”,它使用以下语法:

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)()) { // !!! NEW SYNTAX - POINTER TO MEMBER
        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)()); // !!! ADDED PARENTHESES
        }
        return ret;
    }

public:
    std::vector<float> getAllX() const {
        return get(&Point::getX); // !!! POINTER TO MEMBER
    }
    std::vector<float> getAllY() const {
        return get(&Point::getY); // !!! POINTER TO MEMBER
    }
};

免责声明:未经测试。

此外,您可能想查看中的<functional>C++11;这对这样的事情非常好。

这就是我个人处理这种情况的方式:

#include <functional>
#include <vector>
#include <algorithm>

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(std::function<float(const Point&)> func) const {
        std::vector<float> ret(points.size());
        std::transform(points.begin(), points.end(), ret.begin(), func);
        return ret;
    }

public:
    std::vector<float> getAllX() const {
        return get(std::mem_fn(&Point::getX));
    }

    std::vector<float> getAllY() const {
        return get(std::mem_fn(&Point::getY));
    }
};

免责声明:编译,但未经测试。

于 2013-04-15T16:45:41.087 回答
1

在你的程序中改变了很多东西。指向成员语法的指针与指向函数语法的指针并不完全相同。

我使用了 C++ 中的 typedef 和宏来简化它

http://www.parashift.com/c++-faq/typedef-for-ptr-to-memfn.html

http://www.parashift.com/c++-faq/macro-for-ptr-to-memfn.html

class Point {
    float x, y;
public:
    float getX() const { return x; }
    float getY() const { return y; }
};

// This typedef makes is easier to declare a pointer to a member method
typedef float (Point::*PointPtr)() const;
// This macro makes it easier to call through a member function pointer.
#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember))

class Polygon {
    std::vector<Point> points;

    // Made this a const function. And changed the parameter type.
    std::vector<float> get(PointPtr func) const {
        std::vector<float> ret;

        // Made this a const iterator
        for(std::vector<Point>::const_iterator it = points.begin(); it != points.end(); it++) {
            // Changed the call to use the macro
            ret.push_back(CALL_MEMBER_FN((*it), func)());
        }
        return ret;
    }

public:
    std::vector<float> getAllX() const {
        return get(&Point::getX); 
    }
    std::vector<float> getAllY() const {
        return get(&Point::getY;);
    }
};

解释了评论中的变化。

于 2013-04-15T16:51:58.237 回答