使用函数指针而不事先知道实例

时间:2013-04-15 16:38:20

标签: c++ function-pointers

将以下内容作为示例: (注意,这个例子不起作用,但它应该足以说明我想要做的事情)

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

2 个答案:

答案 0 :(得分: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));
    }
};

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

答案 1 :(得分: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;);
    }
};

解释评论中的变化。