pointer to member function

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

    Here is the syntax for the way you want:

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

    Call it as:

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

提交回复
热议问题