pointer 2 class member function??? how

hello, I don't understand why the code in line 14 doesn't work.
I want to make an array of functions so the user can choose at runtime what function to execute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;
class foo
{
    public:
    void printf(){cout<<"hello world\n";}
};
int main()
{
    void (foo::* px)(void) = {};
	px = &foo::printf; //pointer to member function
	
	foo *te = new foo;
	te->*px(); //this doesn't work why???
	//te->printf();//like this but with pointers ??? 
}


To keep it simple, i Started with one pointer, but it doesn't seem to work...
Please help...
You have to add parentheses to make it work.
 
(te->*px)();
hey thanks a lot. :-) I implemented the array.
So I post it to share with the other people.

1
2
3
4
5
6
7
8
9
int main()
{
        void (foo::* px[5])(void) = {};
        px[0] = &foo::printf; //pointer to member function
	
        foo *te = new foo;
        (te->*px[0])(); 
        delete te;
}


problem solved !!!
Last edited on
Topic archived. No new replies allowed.