一、C语言的回调函数
1.小试牛刀
#include <iostream>
using namespace std;
#include <memory>
#include <stdlib.h>
int add(int a, int b) {
    return a + b;
}
void test01() {
    // 函数指针可以指向任何类型的函数,只要函数的参数列表和返回值类型匹配即可
    int (*pFunc)(int,int) = add;
    // 函数指针可以像普通函数一样被调用,通过函数指针变量名加上括号的方式
    int result = (*pFunc)(1,2);
    cout << result << endl; // 输出 3
}
// typedef 返回类型(*新的函数名)(参数列表)
typedef int (*INT_func)(int,int);
void test02() {
    INT_func pFunc = add;
    int result = pFunc(2,3);
    cout << result << endl; // 输出 5
}
// 回调函数,它允许一个函数作为参数传递给另一个函数
// 这种特性使得我们可以将一些特定的任务委托给其他函数来完成
// 定义一个函数指针类型
typedef void(*Callback)(int);
// 定义一个函数,该函数接受一个回调函数作为参数
void doSomething(Callback callback) {
    cout<<"Doing something..."<<endl;
    // 调用回调函数
    int data = 1024;
    callback(data);
}
// 定义一个回调函数
void printMyData(int data) {
    cout<<"My data is: "<<data<<endl;
}
int main() {
    test01();
    test02();
    // 将回调函数传递给doSomething函数
    /*
        doSomething函数接受一个Callback类型的参数,这是
        一个指向函数的指针.doSomethings函数调用这个回调函数,
        并且将一个整型变量作为参数传递给它
        printMyData在此是一个简单的回调函数,它接受一个整型变量作为
        参数并且把它打印出来
    */
    doSomething(printMyData);
    return 0;
}- 打印结果:
PS D:\Work\c++> ./bin/app     
3
5
Doing something...
My data is: 1024
PS D:\Work\c++>2.动态函数指针
在学习这个知识点的时候,我遇到的坑,非常感谢这位大佬给我指点迷津:
动态函数指针free报错_编程语言-CSDN问答 https://ask.csdn.net/questions/8061857?spm=1001.2014.3001.5505
https://ask.csdn.net/questions/8061857?spm=1001.2014.3001.5505
- micthis大佬写的代码
#include <iostream>
using namespace std;
#include <memory>
#include <stdlib.h>
int add(int a, int b) {
    return a + b;
}
/*
    动态函数指针是指在运行时根据需要动态分配和修改的函数指针
    它可以在程序运行时根据需要指向不同的函数,从而实现更加灵活
    和动态的函数调用
    在c++中,可以使用动态内存分配函数(如malloc或new)来创建
    动态函数指针
*/
int test01() {
    // 创建一个指向函数的指针
    int(**pFunc)(int, int);
    // 使用malloc动态分配内存
    int size = sizeof(int(*)(int, int));
    pFunc = (int(**)(int, int))malloc(size);
    // 将函数指针指向 add函数
    *pFunc = add;
    // 调用函数
    int result = (*pFunc)(2, 3);
    cout << result << endl; // 输出 5
    // 释放内存
    free(pFunc);
    return 0;
}
int main() {
    test01();
    return 0;
}打印结果:
PS D:\Work\c++> ./bin/app
5
PS D:\Work\c++>3.异步编程
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
// A的实现,一般会隐藏
typedef void (*CallbackPtr)(int);// 函数指针定义
typedef struct dataCB{
    int data;
    CallbackPtr callback;
}dataCB;
// 创建实例
dataCB dataCBInstance = {0, NULL};
void* callback_thread(void* arg) { // 此处用的是一个线程
    // 循环改变p->a的值为 0 1 2 3 4 5 6 7 8 9,每个3s改变一次
    dataCB* p = (dataCB*)arg;
    while (1) {
        sleep(3);// 延时3s执行callback函数
        p->callback(p->data);// 函数指针执行函数,这个函数来自于应用层B
        p->data = (p->data + 1) % 10;
    }
}
void startup_app_A() {
    // 创建线程
    pthread_t tid;
    pthread_create(&tid, NULL, callback_thread, (void*)&dataCBInstance);   
}
// 给B的接口,接收注册函数
extern void SetCallBackFun(CallbackPtr cb) {
    printf("SetCallBackFun print! \n");
    dataCBInstance.callback = cb;
}
// //-----------------------应用者B-------------------------------
void recieve(int data)       // 应用者增加的函数,此函数会在A中被执行
{
    //do something
    printf("B得到A的数据 = %d\n",data);
}
int main(void) {
    // 启动A
    startup_app_A();
    SetCallBackFun(recieve);
    // 主函数
    while (1) {
        // std::cout << "main function" << std::endl;
        printf("main function\n");
        sleep(2);
    }
    return 0;
}PS D:\Work\c++> ./bin/app
SetCallBackFun print!
main function
main function
B得到A的数据 = 0
main function
B得到A的数据 = 1
main function
main function
B得到A的数据 = 2
main function
B得到A的数据 = 3
main function
main function
B得到A的数据 = 4
main function
B得到A的数据 = 5
main function
main function
B得到A的数据 = 6
main function
B得到A的数据 = 7
main function
main function
B得到A的数据 = 8
main function
B得到A的数据 = 9
main function
main function
B得到A的数据 = 0
main function
B得到A的数据 = 1
main function
main function
B得到A的数据 = 2
main function
B得到A的数据 = 3
main function
main function
B得到A的数据 = 4
main function
B得到A的数据 = 5
main function
main function
B得到A的数据 = 6
main function
B得到A的数据 = 7
main function
main function
B得到A的数据 = 8
main function
B得到A的数据 = 9
main function
main function
B得到A的数据 = 0
main function参考文章:C/C++面向对象(OOP)编程-回调函数详解(回调函数、C/C++异步回调、函数指针)-CSDN博客 https://blog.csdn.net/m0_47324800/article/details/135315345
https://blog.csdn.net/m0_47324800/article/details/135315345
二、C++回调函数
1.动态函数指针
#include <iostream>
using namespace std;
#include <memory>
#include <stdlib.h>
int add(int a, int b) {
    return a + b;
}
/*
    动态函数指针是指在运行时根据需要动态分配和修改的函数指针
    它可以在程序运行时根据需要指向不同的函数,从而实现更加灵活
    和动态的函数调用
    在c++中,可以使用动态内存分配函数(如malloc或new)来创建
    动态函数指针
*/
typedef int(*handleFunc)(int,int);
int test01() {
    // 创建一个指向函数的指针
    int(**pFunc)(int, int);
    pFunc = new handleFunc;
    // 将函数指针指向 add函数
    *pFunc = add;
    // 调用函数
    int result = (*pFunc)(2, 3);
    cout << result << endl; // 输出 5
    // 释放内存
    delete pFunc;
    pFunc = nullptr;
    return 0;
}
int main() {
    test01();
    return 0;
}2.简单回调
#include <iostream>
#include <functional>
// 定义一个回调函数类型
typedef std::function<void(int)> Callback;
// 定义一个接受回调函数的函数
void process(int value,Callback callback) {
    std::cout<<"传入处理值: "<<value<<std::endl;
    callback(value); // 调用回调函数
}
// 定义一个回调函数
int add(int value) {
    value += 10;
    std::cout<<"传出结果值: "<<value<<std::endl;
    return value;
}
int main() {
    int value = 42;
    process(value,add); // 传递回调函数给process函数
    return 0;
}执行结果:
PS D:\Work\c++> ./bin/app
传入处理值: 42
传出结果值: 52
PS D:\Work\c++> 
未完待续 ~



















