【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

相关推荐
yaoxin52112317 小时前
434. Java 日期时间 API - Period 基于日期的时间段
java·开发语言·python
凡人叶枫18 小时前
Effective C++ 条款30:透彻了解 inlining 的里里外外
linux·开发语言·c++·嵌入式开发·effective c++
noipp18 小时前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
学逆向的18 小时前
C++纯虚函数
开发语言·c++·网络安全
程序员二叉19 小时前
【JUC】ThreadLocal底层原理|内存泄漏|弱引用|跨线程传递方案
java·开发语言·面试·职场和发展·juc
程序员二叉19 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
凡人叶枫19 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
Qt程序员20 小时前
掌握 Linux 内核调度:从原理到实现(进程篇)
java·开发语言
code bean20 小时前
【LangChain】检索器完全指南:从向量检索到生产级 RAG 架构
java·开发语言·微服务
LabVIEW开发20 小时前
LabVIEW + MATLAB 混合编程:爆炸场测试数据精准采集方案
开发语言·matlab·labview