pointer to member function

前端 未结 5 1952
醉梦人生
醉梦人生 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:34

    A member function requires a pointer to the object instance. That is, think of getX as string Row::getX(const Table *this). You need to bind the member function with an instance placeholder.

    E.g. using tr1,

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

    The binding creates a functor that takes in a Row object, assuming that your filter function is correctly defined. You should show code for filter. I presume it should be:

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

提交回复
热议问题