【C++】传递‘类非静态成员函数’用作回调函数

在C语言中,传递函数指针是非常常见的操作。

在C++语言中,使用C语言一致的方法传递全局函数指针,或者传递静态函数指针也很常见。

不过如果遇到想传递非静态成员函数时,可以参考以下示例代码。

cpp 复制代码
#ifndef _WORKER_HPP_
#define _WORKER_HPP_

#include <iostream>
#include <unistd.h>
#include <functional>
#include <chrono>
#include <iomanip>
#include <sstream>


class Worker {
public:
    // 设置回调函数
    void registerCallback(std::function<void(int, std::string, long)> cb) {
        this->mCallback = cb;
    }

    void startWork() {
        using namespace std::literals;

        const std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();
        const std::time_t t_c = std::chrono::system_clock::to_time_t(now);

        int i = 1008;
        std::stringstream ss;
        ss << std::put_time(std::localtime(&t_c), "%F %T");
        std::string s = ss.str();
        long l = __cplusplus;

        mCallback(i, s, l);
    }

private:
    std::function<void(int, std::string, long)> mCallback;

};

#endif

参考Manager内的work函数,列出了几种写法。

cpp 复制代码
#ifndef _MANAGER_HPP_
#define _MANAGER_HPP_

#include <functional>
#include <string>
#include <iostream>

#include "worker.hpp"

class Manager {
public:

    Manager(): mI(-1), mS("coco"), mL(-1L) {
        
    }

    virtual ~Manager() = default;

public:
    void work() {
        using namespace std::placeholders;
        // 设置回调函数, 使用lambda
        worker.registerCallback([this](int&& i, std::string&& s, long&& l) -> void {
            this->onMsgCallback(i, s, l);
        });

        // 设置回调函数,使用bind,搭配mem_fn
        auto ptr = std::mem_fn(&Manager::onMsgCallback);
        worker.registerCallback(std::bind(ptr, this, _1, _2, _3));

        // 不搭配mem_fn
        worker.registerCallback(std::bind(&Manager::onMsgCallback, this, _1, _2, _3));

        worker.startWork();
    }

    void print() {
        std::cout << __FUNCTION__ << " mI is " << mI << ", mS is " << mS << ", mL is " << mL << std::endl;
    }

private:
    void onMsgCallback(int i, std::string s, long l) {
        std::cout << __FUNCTION__ << " i is " << i << ", s is " << s << ", l is " << l << std::endl;
        this->mI = i;
        this->mS = s;
        this->mL = l;
    }

private:
    int mI;

    std::string mS;

    long mL;

    Worker worker;
};

#endif

main示例:

cpp 复制代码
int main()
{
    // 演示将非静态成员函数设置为回调函数
    {
        Manager manager;
        manager.print();
        manager.work();
        manager.print();
    }
    return 0;
}

输出参考:

print mI is -1, mS is coco, mL is -1

onMsgCallback i is 1009, s is 2023-11-18 20:22:34, l is 201402

print mI is 1009, mS is 2023-11-18 20:22:34, mL is 201402

相关推荐
王老师青少年编程6 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
西门吹-禅9 小时前
java springboot N+1问题
java·开发语言·spring boot
skywalk81639 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
2023自学中10 小时前
C++ 内存追踪器
linux·c++
IT笔记10 小时前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
2zcode11 小时前
免费开源项目文档:基于MATLAB卷积神经网络的口罩佩戴检测系统
开发语言·matlab·cnn
逝水无殇11 小时前
C# 运算符重载详解
开发语言·后端·c#
TPBoreas11 小时前
配置信息防泄露方案:.env 环境隔离详解(dotenv-java)
java·开发语言
敲代码的嘎仔12 小时前
实习日志day6--实习日志day6--title命名规范化&businessType纠正&补充缺失的@Log注解&报警与通信模块补充&产出阶段总结文档
java·开发语言·人工智能·git·python·实习·大二
江华森12 小时前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python