Java c C + + callback function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.