c++ - pointer to member function -


i trying generalize functions filterx() , filtery() in following class table function filter().

the functions filterx() , filtery() differ in function call inside procedure. while filterx() calls getx(), filtery() calls 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(); != 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(); != 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(); != vx.end(); ++it)         cout << *it << "\t";     cout << endl;      cout << "matching y" << endl;     for(it = vy.begin(); != vy.end(); ++it)         cout << *it << "\t";     cout << endl;      return 0; } 

i tried pointer member function got bogged down compiler errors. following example, have following main() if possible:

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 pass getx     // function named filter            vector<int> vx = t.filter("x1", getx);     vector<int> vy = t.filter("y2", gety);      return 0; } 

here syntax way want:

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

call as:

vector<int> vx = t.filter("x1", &row::getx); vector<int> vy = t.filter("y2", &row::gety); 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -