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

P

Pawel_Iks

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::eek:therFun(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)
 
V

Victor Bazarov

Pawel_Iks said:
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::eek:therFun(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
 
O

osmium

Pawel_Iks said:
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::eek:therFun(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-lite/#table-of-contents
 
T

terminator

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::eek:therFun(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::eek:therFunc' takes a static
function as its parameter.

you can overload 'A::eek:therFunc' with this one:

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

int A::eek:therFunc (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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top