Hi folks, I am learning member function pointers, wrote following program to understand typedef of function (of other class eg: test) inside other class(eg: exam) and then its access.:smug: I have tried to use (e.*m)(8) but its gave err:‘m’ was not declared in this scope Could you please tell me how to call function 'add' using e.m I have added comments inline for reference Code: #include<iostream> using namespace std; class test { public: int add(int k){cout<<"test::add"<<endl; return k+1; } }; class exam { public: typedef int (test::*ptr)(int); ptr m; }; int main() { exam e; test t; e.m = &test::add; (e.m)(8); //Err:must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘e.exam::m (...)’, e.g. ‘(... ->* e.exam::m) (...)’ /** followed (function_ptr)(arg) . what is the correct way to call add using e.m **/ return 0; }
U need to mention he class name of which the add() function is the member... Modify line (e.m)(8); To t .*(e.m) (8)
Please consider this on U need to mention he class name of which the add() function is the member... Modify line (e.m)(8); To (t .*(e.m)) (8)