1. Define the function pointer of the CALLBACK type typedef int (callback*) (int x, int y)
2, define the function method, the function pointer of CALLBACK type as function parameter void method (CALLBACK FP, int a, int b)
void method (CALLBACK FP, int a, int b)
{
FP (A, b);
}
3. Define the main function and call method
int main ()
{
Calling the methods method requires a parameter FP, so you write a function of that type and implement the callback.
int ret = Method (Add, 3, 5)
return 0;
}
4, Int (*add) (int x, int y)
{
return x + y;
}
The first step of using interface simulation function pointer in Java
Public interface Functionpoint {
int callback (int x, int y);
}
Defines a class that implements the second step of the interface object as a method parameter
public class Function {
public int Method (Functionpoint FP, int x, int y) {
Fp.callback (x, y);
}
}
Defining a class with the Main method
public class Calllback implements Functionpoint {
public static void Main (string[] args) {
Call method Methods
function f = new function ();
F.method (New Imp (), 2, 5);
}
}
public class Imp implements Functionpoint {
public int callback (int x, int y) {
return x + y;
}
}
C + + Callback reference: http://blog.csdn.net/xie1xiao1jun/article/details/8262902
This article is from the "Ai-life" blog, make sure to keep this source http://ai00life.blog.51cto.com/2198685/1932493
Java c C + + callback function