C + +-CallBack

Source: Internet
Author: User

This is the lab building on the previous callback debug example, I did not submit the validation, but running on-premises without any problems, and no warning:

#include <stdio.h> #define MAX 3typedef Int (*alarm) (int type); alarm Alarm_list[max];int index = 0;void Register_ala RM (alarm a); int hit_alarm (int index), void Register_alarm (alarm a) {    alarm_list[index++] = A;} int hit_alarm (int index) {    if (Index < 0 | | index >= MAX)        return 1;    (*alarm_list[index]) (index);    return 0;} int alarm1 (int type) {    printf ("one:%d\n", type);    return 0;} int alarm2 (int type) {    printf ("two:%d\n", type);    return 0;} int alarm3 (int type) {    printf ("three:%d\n", type);    return 0;} int main{    register_alarm (ALARM1);    Register_alarm (ALARM2);    Register_alarm (ALARM3);    Hit_alarm (0);    Hit_alarm (1);    Hit_alarm (2);    return 0;}

Write another example of implementing a callback:

#include <stdio.h>typedef int (*__callback__) (void* type_param), void Func_register (__callback__ func, void* param), void Func_register (__callback__ func, void* param) {    (*func) (param);} Custom Function int My_func (void* param) {    printf ("Param is%d\n", (int*) param);    return 0;} int main{    //Custom function is invoked as a function parameter    func_register (My_func, (void*) 5);    return 0;}

  

  

C + +-CallBack

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.