By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

how to call method of the class which contains a pointer to other class method?

P: n/a
I have following class definition (A.h file):

class A
{
public:
int N;
int count1(int n) {n++;}
int count2(int n) {n+=5;}
int otherFun(int n, int (*fun)(int));
}

and I want to implement otherFun method (in A.cpp file):

int A::otherFun(int,int (*fun)(int))
{
int c=0,d;
d=fun(c);
}

and call it (in main.cpp):

int main()
{
A obj=A();
int t;
A.N=12;
t=A.otherFun(N,test.count1);
return 0;
}

and it doesn't work ... I have two questions:
1) If there implementation of otherFun method is correct, and when
it's correct how can i call this method in main() function?
2) why it doesn't work? (for simply function - not class member - it
works fine)

Jul 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pawel_Iks wrote:
I have following class definition (A.h file):

class A
{
public:
int N;
int count1(int n) {n++;}
int count2(int n) {n+=5;}
int otherFun(int n, int (*fun)(int));
}

and I want to implement otherFun method (in A.cpp file):

int A::otherFun(int,int (*fun)(int))
{
int c=0,d;
d=fun(c);
}

and call it (in main.cpp):

int main()
{
A obj=A();
int t;
A.N=12;
t=A.otherFun(N,test.count1);
The usual beginner's mistake: a non-static member function is NOT
compatible with a pointer to function. Look up and read about
"pointer to member".
return 0;
}

and it doesn't work ... I have two questions:
1) If there implementation of otherFun method is correct, and when
it's correct how can i call this method in main() function?
You can't.
2) why it doesn't work? (for simply function - not class member - it
works fine)
Sure. There is a difference between a function and a member function
that is not static. You need to educate yourself on member funcitons
and "pointers to members".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '07 #2

P: n/a
"Pawel_Iks" wrote:
>I have following class definition (A.h file):

class A
{
public:
int N;
int count1(int n) {n++;}
int count2(int n) {n+=5;}
int otherFun(int n, int (*fun)(int));
}

and I want to implement otherFun method (in A.cpp file):

int A::otherFun(int,int (*fun)(int))
{
int c=0,d;
d=fun(c);
}

and call it (in main.cpp):

int main()
{
A obj=A();
int t;
A.N=12;
t=A.otherFun(N,test.count1);
return 0;
}

and it doesn't work ... I have two questions:
1) If there implementation of otherFun method is correct, and when
it's correct how can i call this method in main() function?
2) why it doesn't work? (for simply function - not class member - it
works fine)
Try clicking on item 33 in this link.

http://www.parashift.com/c++-faq-lit...le-of-contents
Jul 30 '07 #3

P: n/a
On Jul 30, 3:39 pm, Pawel_Iks <pawel.labed...@gmail.comwrote:
I have following class definition (A.h file):

class A
{
public:
int N;
int count1(int n) {n++;}
int count2(int n) {n+=5;}
int otherFun(int n, int (*fun)(int));

}

and I want to implement otherFun method (in A.cpp file):

int A::otherFun(int,int (*fun)(int))
{
int c=0,d;
d=fun(c);

}

and call it (in main.cpp):

int main()
{
A obj=A();
this works but it is better to simply write:
A obj;
int t;
A.N=12;
t=A.otherFun(N,test.count1);
this syntax belongs to C# not C++. instance member functions are
different from static functions the 'A::otherFunc' takes a static
function as its parameter.

you can overload 'A::otherFunc' with this one:

class A{
public:
int otherFunc (int,A& ,int(A::*)(int) );
//the rest of class follows as before:
...
};

int A::otherFunc (int n,A& obj_ref,int(A::*method)(int) ){
return (obj_ref.*method)(n);//call with object or referenc
};

int F(int){};
static int G(int){};

int main(){
A a,test;
a.otherFunc (1,test,&A::count1);//my version of otherFun
a.otherFunc (2,F);//your version of otherFun
a.otherFunc (3,G);//your version of otherFun
return 0;
};
2) why it doesn't work? (for simply function - not class member - it
works fine)
because you think that combination of a method with an object forms a
static function but you are wrong.
It is obvious that you are migrating from a newly introduced high-
level language(java/C#) to the mixed-level C++.Things are different in
different languages.

regards,
FM.
Jul 31 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.