I am trying to generalize the functions filterX() and filterY() in the following class Table to function filter().
Here is how to do it by using pointer to member functions:
// 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
vector filter(string s)
{
int i = (d_table[i].*getter)(); // how to use getter in filter
}
private:
vector d_table;
};
// Usage:
Table t;
t.filter<&Row::GetX>("");