Another question

Can I pass a member function of a class as a parameter to another function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>
#include <vector>

class Window {
    vector<string> screens;
public:
    typedef vector<string>::size_type index;
    void clear(index);
};
void RefreshScreen((*pw)(Window::index)) {
    pw(0);
}

int main()
{
    Window testwindow;
    RefreshScreen(testwindow.clear);    //won't compile
    return 0;
}

You need the parentheses on line 17:

RefreshScreen( testwindow.clear() );
I've tried, the compiler produce an error: non-standard syntax; use '&' to create a pointer to member. Actually, testwindow.clear should have already been a pointer, weird complier, I've met several times before.
You have to also pass a value to testwindow.clear().
yeah, I've tried that too, for instance: RefreshScreen(testwindow.clear(0)). Neither of them would compile. Actually, testwindow.clear(0) yields a value, which is void here, not a pointer, as you know.
Last edited on
Can you show your latest code?
Topic archived. No new replies allowed.