pointer to member function

前端 未结 5 1958
醉梦人生
醉梦人生 2021-01-20 13:56

I am trying to generalize the functions filterX() and filterY() in the following class Table to function filter().

5条回答
  •  太阳男子
    2021-01-20 14:47

    Modify your code here:

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

    Here:

    vector Table::filter(string s, string (Row::*f)() const)
    {   
        vector result;
        vector::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;
    }
    

    And here:

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

提交回复
热议问题