当前位置: 首页 > article >正文

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

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

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

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

#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函数,列出了几种写法。

#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示例:

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


http://www.kler.cn/news/136139.html

相关文章:

  • 数据结构与算法-图
  • ElasticSearch在Windows上的下载与安装
  • flask创建步骤
  • Excel 文件比较工具 xlCompare 11.01 Crack
  • 深度学习人体跌倒检测 -yolo 机器视觉 opencv python 计算机竞赛
  • WebSocket协议详解
  • linux rsyslog综合实战1
  • CI/CD相关概念学习
  • Apache POI(Java)
  • flask实现session开发
  • 1. hadoop环境准备
  • 基于一致性算法的微电网分布式控制MATLAB仿真模型
  • Java格式化类Format
  • 电子学会C/C++编程等级考试2022年06月(一级)真题解析
  • 初识分布式键值对存储etcd
  • Zotero在word中插入带超链接的参考文献/交叉引用/跳转参考文献
  • 工作记录---为什么双11当天不能申请退款?(有趣~)
  • 万字长文 - Python 日志记录器logging 百科全书 - 高级配置之 日志分层
  • 2023-11-18 mysql-sysbench压测TPS/QPS-记录
  • LangChain 5易速鲜花内部问答系统