指向成员函数的指针

时间:2012-03-08 03:42:39

标签: c++ function-pointers

我正在尝试将以下filterX()中的函数filterY()class Table概括为函数filter()

函数filterX()filterY()仅在程序中调用的函数不同。在filterX()拨打getX()时,filterY()会拨打getY()

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Row 
{
    public:
        void add(string x, string y, int val);
        string getX()   const { return d_x; } 
        string getY()   const { return d_y; } 
        int    getVal() const { return d_val; } 

    private:
        string d_x;
        string d_y;
        int    d_val;
};

class Table
{
    public:
        void add(string x, string y, int val);
        vector<int> filterX(string s);
        vector<int> filterY(string s);
    private:
        vector<Row> d_table;
};


//--------------------class Row----------------------------
void Row::add(string x, string y, int val)
{
    d_x   = x;
    d_y   = y;
    d_val = val;
}


//-------------------class Table---------------------------

void Table::add(string x, string y, int val)
{
    Row r;
    r.add(x, y, val);
    d_table.push_back(r);
}

vector<int> Table::filterX(string s)
{
    vector<int> result;
    vector<Row>::iterator it;
    for(it = d_table.begin(); it != d_table.end(); ++it) {
        if(it->getX() == s) {
            int val = it->getVal();
            result.push_back(val);
        }
    }
    return result;
}


vector<int> Table::filterY(string s)
{
    vector<int> result;
    vector<Row>::iterator it;
    for(it = d_table.begin(); it != d_table.end(); ++it) {
        if(it->getY() == s) {
            int val = it->getVal();
            result.push_back(val);
        }
    }
    return result;
}

int main()
{
    Table t;
    t.add("x1", "y1", 1);
    t.add("x1", "y2", 2);
    t.add("x2", "y1", 3);
    t.add("x2", "y2", 4);

    vector<int> vx = t.filterX("x1");
    vector<int> vy = t.filterY("y2");

    vector<int>::const_iterator it;

    cout << "Matching X" << endl;
    for(it = vx.begin(); it != vx.end(); ++it)
        cout << *it << "\t";
    cout << endl;

    cout << "Matching Y" << endl;
    for(it = vy.begin(); it != vy.end(); ++it)
        cout << *it << "\t";
    cout << endl;

    return 0;
}

我尝试了指向成员函数的指针,但是由于编译器错误而陷入困境。对于以下示例,如果可能,我希望拥有以下main()

int main()
{
    Table t;
    t.add("x1", "y1", 1);
    t.add("x1", "y2", 2);
    t.add("x2", "y1", 3);
    t.add("x2", "y2", 4);

    // instead of filterX, need to pass getX
    // to a function named filter       
    vector<int> vx = t.filter("x1", getX);
    vector<int> vy = t.filter("y2", getY);

    return 0;
}

5 个答案:

答案 0 :(得分:2)

成员函数需要指向对象实例的指针。也就是说,将getX视为string Row::getX(const Table *this)。您需要使用实例占位符bind成员函数。

E.g。使用tr1,

vector<int> vx = t.filter("x1", std::bind(&Row::getX, std::placeholders::_1));

绑定会创建一个带有Row对象的仿函数,假设您的过滤器功能已正确定义。您应该显示filter的代码。我认为它应该是:

template <class function>
vector<int> Table::filter(const string &s, function f)
{
    vector<int> result;
    for (vector<Row>::const_iterator it = d_table.begin(), tEnd = d_table.end();
        it != tEnd; ++it)
    {
        if (s == f(*it)) result.push_back(it->getVal());
    }
    return result;
}

答案 1 :(得分:1)

以下是使用指向成员函数的指针:

// helper to avoid type nightmare;
typedef string (Row::* GetterP)() const;

class Table 
{ 
public: 
    void add(string x, string y, int val); 

    // Define a templated function that can be called with GetX or GetY
    template <GetterP getter>
    vector<int> filter(string s)
    {
         int i = (d_table[i].*getter)(); // how to use getter in filter
    }

private:
    vector<Row> d_table;  
};

// Usage:
Table t;
t.filter<&Row::GetX>("");

答案 2 :(得分:1)

如果您想使用显式PMF(或至少看看它们是如何使用的):

声明PMF类型:

class Row 
{
    public:
    typedef string (Row::*getFilter)() const;
     // etc.
};

class Table
{
    public:
    // Call it like this:

    vector<int> pmf_filterX(string s)
    {
        return filter(s, &Row::getX);
    }
    private:
    // Use it like this:
    vector<int> filter(string s, Row::getFilter f)
    {
        vector<int> result;
        vector<Row>::iterator it;
        for(it = d_table.begin(); it != d_table.end(); ++it)
            {
            const Row& row = *it;

            if ((row.*f)() == s) 
                {
                int val = it->getVal();
                result.push_back(val);
                }
            }
        return result;
    }
};

答案 3 :(得分:1)

以下是您想要的语法:

vector<int> Table::filter(string s, string (Row::*get)() const)
{                                  //^^^^^^^^^^^^^^^^^^^^^^^^^^^ member pointer
  ...
  if(((*it).*get)() == s) {  // call using the (*it). and not it->
  ... 
}

将其命名为:

vector<int> vx = t.filter("x1", &Row::getX);
vector<int> vy = t.filter("y2", &Row::getY);

答案 4 :(得分:1)

在此处修改您的代码:

public:
    void add(string x, string y, int val);
    vector<int> filter(string s, string (Row::*)() const);
private:
    ...

下面:

vector<int> Table::filter(string s, string (Row::*f)() const)
{   
    vector<int> result;
    vector<Row>::iterator it;
    for(it = d_table.begin(); it != d_table.end(); ++it) {
        if((*it.*f)() == s) {
            int val = it->getVal();
            result.push_back(val); 
        }
    }
    return result;
}

在这里:

int main()
{
    ...
    vector<int> vx = t.filter("x1", &Row::getX);
    vector<int> vy = t.filter("y2", &Row::getY);

    ...