C++ 函数指针

  • 函数指针是指向函数的指针变量,它可以用来存储函数的地址,从而允许在程序中动态地调用不同的函数。
  • 定义函数指针的一般语法如:返回类型 (*指针变量名)(参数类型1, 参数类型2, …); // void (*failedCallBack)(int)

  • 回调函数: 函数指针常用于实现回调机制,其中一个函数作为参数传递给另一个函数,以便在特定事件发生时调用。比如,在图形界面库中,可以通过函数指针来定义按钮点击事件的处理函数。
void btnListenerSuccess() {
    std::cout << "btnListenerSuccess" << std::endl;
}

void btnListenerFailed(int code) {
    std::cout << "btnListenerFailed" << std::endl;
}

void RegisterButtonClick(void (*successCallBack)(), void (*failedCallBack)(int)) {
    successCallBack();
    failedCallBack(100);
}

int main() {
    RegisterButtonClick(btnListenerSuccess, btnListenerFailed);
    return 0;
}
  • 策略模式: 函数指针可以用于实现策略模式,通过在运行时选择不同的函数来改变算法的行为。
typedef int (*MathOperation)(int, int);

int Add(int x,int y) {
    return x + y;
}

int Subtract(int x, int y) {
    return x - y;
}

int Calculate(MathOperation operation, int x, int y) {
    return operation(x, y);
}

int main() {
    MathOperation addFunc = Add;
    MathOperation subtractFunc = Subtract;

    std::cout << "Addition result: " << Calculate(addFunc, 5, 3) << std::endl;
    std::cout << "Subtraction result: " << Calculate(subtractFunc, 8, 4) << std::endl;
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
  • 动态加载函数库: 函数指针可以用于动态加载共享库(动态链接库)中的函数,从而实现插件式架构。
    • dl 为 dynamic linking 的缩写
#include <iostream>
#include <dlfcn.h>  //动态链接库相关的头文件

typedef void (*PluginFunction)();

int main() {
    std::string currentPath = __FILE__; // 获取当前源文件的绝对路径
    size_t lastSlash = currentPath.rfind('/');
    std::string libraryPath = currentPath.substr(0, lastSlash + 1) + "libTest.so"; // 构建共享库的绝对路径
    //libraryPath: /Users/dxd/workspace/github/Cambodia/libTest.so
    std::cout << "so libraryPath: "<< libraryPath << std::endl;

    std::string libraryPathUntitled = currentPath.substr(0, lastSlash + 1) + "libuntitled.dylib";
    std::cout << "dylib libraryPathUntitled: "<< libraryPathUntitled << std::endl;

    //加载动态库 RTLD_LAZY 表示在运行时进行懒惰链接,即在实际使用到库函数时再进行加载和链接。
    void* pluginHandle = dlopen(libraryPathUntitled.c_str(), RTLD_LAZY);
    std::cout << "pluginHandle: "<< pluginHandle << std::endl;
    if (pluginHandle) {
        //这行代码使用 dlsym 函数从已加载的共享库中获取名为 PluginFunction 的函数的地址,并将其赋值给 pluginFunc 函数指针。
        PluginFunction pluginFunc = (PluginFunction)dlsym(pluginHandle, "hello");
        std::cout << "pluginFunc: "<< pluginFunc << std::endl;
        if (pluginFunc) {
            pluginFunc();
        } else {
            std::cout << "Failed to find function in plugin." << std::endl;
        }
        //关闭先前通过 dlopen 打开的共享库,释放相关资源。
        dlclose(pluginHandle);
    } else {
        std::cout << "Failed to load plugin." << std::endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值