For example: Int (*foo) (int arg), remember to distinguish it from another pointer function, like this: int *foo (int arg).
For example, we can declare a variable and a function like this:
Copy Code code as follows:
Int (*pfun) (int arg) = 0;
int fun (int arg); This function is implemented casually, I will not write.
If we want to take advantage of the function pointer manipulation function, we will use the same as the pointer variable:
Copy Code code as follows:
Pfun=fun;
int result= (*pfun) (123);
Yes, it's not necessary to have a chicken ribs. This is, of course, because we are useless in the right place. Here's what I'm going to talk about is using a class to call a member of another unrelated class.
Code:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Template<typename T,typename n>
Class functor{
Public
Functor (t *otherp,n (t::* otherfun) (N Arg))
{
Mp=otherp;
Mfun=otherfun;
}
Virtual n operator () (n Arg)
{
Return (*mp.*mfun) (ARG);
}
Private
N (T::* mfun) (n Arg);
T *mp;
};
Class a{
Public
A (int A0): A (A0) {}
int traced (int b)
{
cout<< "Trace a=" <<a<< ", b=" <<b<<endl;
return 0;
}
Private
int A;
};
int main ()
{
A A (10);
Functor<a,int> Trace (&a,&a::traced);
Trace (5);
return 0;
}
Line 33rd passes the member function address of Class A to the functor function pointer, which enables the members of a functor to be processed through the members of a.
Here we use an overload of operator (), which can be replaced by another function to handle the functor function pointer.
(It doesn't work, but the function pointer is very human, not intuitive), like this:
Copy Code code as follows:
#include <iostream>
using namespace Std;
Template<typename T,typename n>
Class functor{
Public
Functor (t *otherp,n (t::* otherfun) (N Arg))
{
Mp=otherp;
Mfun=otherfun;
}
Virtual n Out (n arg)//change
{
Return (*mp.*mfun) (ARG);
}
Private
N (T::* mfun) (n Arg);
T *mp;
};
Class a{
Public
A (int A0): A (A0) {}
int traced (int b)
{
cout<< "Trace a=" <<a<< ", b=" <<b<<endl;
return 0;
}
Private
int A;
};
int main ()
{
A A (10);
Functor<a,int> Trace (&a,&a::traced);
Trace.out (5); Changes
return 0;
}
C + + is really complicated, but if we use it well, complexity is powerful.