Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   access 'typedef'ed member function (http://www.go4expert.com/forums/access-typedefed-function-t28667/)

pushpat 4Jul2012 12:51

access 'typedef'ed member function
 
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;
}


debugEnthu 5Jul2012 16:02

Re: access 'typedef'ed member function
 
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)

debugEnthu 5Jul2012 19:31

Re: access 'typedef'ed member function
 
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 9Jul2012 10:54

Re: access 'typedef'ed member function
 
thank you debugEnthu, its working :)


All times are GMT +5.5. The time now is 23:19.