On Jul 30, 3:39 pm, Pawel_Iks <pawel.labed...@gmail.com> 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:
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:

therFunc' takes a static
function as its parameter.
you can overload 'A:

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

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.