access 'typedef'ed member function

pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
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.

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;
}

Last edited by pushpat; 4Jul2012 at 14:18..
debugEnthu's Avatar
Go4Expert Member
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)
shabbir like this
debugEnthu's Avatar
Go4Expert Member
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)
pushpat's Avatar, Join Date: Feb 2012
Go4Expert Member
thank you debugEnthu, its working