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

沈阳网站制作列表网wordpress 屏蔽ip插件

沈阳网站制作列表网,wordpress 屏蔽ip插件,推荐小蚁人网站建设,com域名注册查询1 概述 回调函数是一种编程模式,指的是将一个函数作为参数传递给另一个函数,并在某个特定事件发生时或满足某些条件时由该函数调用。这种机制允许你定义在特定事件发生时应执行的代码,从而实现更灵活和模块化的程序设计。 2 传统C/C回调实现…

1 概述

  • 回调函数是一种编程模式,指的是将一个函数作为参数传递给另一个函数,并在某个特定事件发生时或满足某些条件时由该函数调用。
  • 这种机制允许你定义在特定事件发生时应执行的代码,从而实现更灵活和模块化的程序设计。

2 传统C/C++回调实现方式

  • 传统C/C++实现回调,主要通过函数指针来实现。
  • 有这样一个场景,某业务系统需要检测环境温度,当温度大于50度时进行告警,告警接口就可以作为回调函数,在温度大于50度时调用。这里通过随机生成一个温度值,模式现实场景。
  • 示例
    •   #include <iostream>#include <stdlib.h>#include <time.h>// 定义函数指针typedef void(*pCb)(int);void BusProcess(int tempera, pCb cb) {printf("开始业务\n");printf("业务处理中\n");// 处理业务过程中,如果温度值大于50摄氏度,则调用告警接口进行告警if (tempera > 50) {cb(tempera);}printf("结束业务\n");}// 定义回调函数void temWarning(int tempera) {printf("温度值为: %d 已超阈值,告警 ...\n", tempera);}int main() {{srand(time(NULL));  // 随机生成温度值int tempera = rand() % 100;// 开启业务BusProcess(tempera, temWarning);}system("pause");return 0;}
      
  • 打印结果
    •   开始业务业务处理中温度值为: 65 已超阈值,告警 ...结束业务请按任意键继续. . .
      

3 C++11提供的回调新实现方式

  • 介绍C++11实现回调前先介绍C++11提供的两个新接口std::functionstd::bind

3.1 std::function

  • std::function是一个通用的函数包装器,可以存储任何可调用对象,包括普通函数、类成员函数、仿函数、Lambda表示式。
  • 基本语法
    •   #include <functional>std::function<返回值类型(参数类型列表)> 函数对象;
      
  • 示例
    •   #include <iostream>#include <functional>// 普通函数int add(int a, int b) {return a + b;}class Multiply {public:int operator()(int a, int b) {return a * b;}};int main() {// 存储普通函数std::function<int(int, int)> func1 = add;std::cout << "func1 result: " << func1(3, 4) << std::endl;// 存储 Lambda 表达式std::function<int(int, int)> func2 = [](int a, int b) { return a - b; };std::cout << "func2 result: " << func2(10, 3) << std::endl;// 存储函数对象Multiply multiply;std::function<int(int, int)> func3 = multiply;std::cout << "func3 result: " << func3(5, 6) << std::endl;return 0;}
      

3.2 std::bind

  • std::bind是一个函数模板,用于将函数或成员函数与其参数绑定,生成一个新的可调用对象。
  • 基本语法
    •   // 绑定非类成员函数/变量auto f = std::bind(可调用对象地址, 绑定的参数/占位符);// 绑定类成员函/变量auto f = std::bind(类函数/成员地址, 类实例对象地址, 绑定的参数/占位符);
      
  • 示例
    •   #include <iostream>#include <functional>int add(int a, int b) {return a + b;}class MyClass {public:int multiply(int a, int b) {return a * b;}};int main() {// 绑定普通函数auto boundAdd = std::bind(add, std::placeholders::_1, std::placeholders::_2);std::cout << "Result: " << boundAdd(5, 10) << std::endl; // 输出 15MyClass obj;// 绑定类成员函数auto boundMultiply = std::bind(&MyClass::multiply, &obj, std::placeholders::_1, std::placeholders::_2);std::cout << "Result: " << boundMultiply(3, 4) << std::endl; // 输出 12system("pause");return 0;}
      

3.3 C++11实现回调

  • 介绍完std::functionstd::bind再看下如何使用C++11语法实现回调。

  • 回调函数为普通函数时示例

    •   #include <iostream>#include <stdlib.h>#include <time.h>#include <functional>void BusProcess(int tempera, std::function<void(int)> op) {printf("开始业务\n");printf("业务处理中\n");// 处理业务过程中,如果温度值大于50摄氏度,则调用告警接口进行告警if (tempera > 50) {op(tempera);}printf("结束业务\n");}void temWarning(int tempera) {printf("温度值为: %d 已超阈值,告警 ...\n", tempera);}int main() {{srand(time(NULL));  // 随机生成温度值int tempera = rand() % 100;// 开启业务,调用对象为普通函数BusProcess(tempera, temWarning);}system("pause");return 0;}
      
  • 打印结果

    •   开始业务业务处理中温度值为: 56 已超阈值,告警 ...结束业务请按任意键继续. . .
      
  • 回调函数为类成员对象、函数对象、Lambda时示例

    •   #include <iostream>#include <stdlib.h>#include <time.h>#include <functional>void BusProcess(int tempera, std::function<void(int)> op) {printf("开始业务\n");printf("业务处理中\n");// 处理业务过程中,如果温度值大于50摄氏度,则调用告警接口进行告警if (tempera > 50) {op(tempera);}printf("结束业务\n");}class MyWarn {public:void startWarning(int tempera) {printf("温度值为: %d 已超阈值,告警 ...\n", tempera);}void operator()(int tempera) {printf("operator() 温度值为: %d 已超阈值,告警 ...\n", tempera);}};int main() {{srand(time(NULL));// 随机生成温度值int tempera = rand() % 100;MyWarn mwarn;std::function<void(int)> fc = std::bind(&MyWarn::startWarning, &mwarn, std::placeholders::_1);// 调用对象为类成员函数BusProcess(tempera, fc);MyWarn mwarn2;std::function<void(int)> fc2 = mwarn2;// 调用对象为仿函数BusProcess(tempera, fc2);// 调用对象为Lambda表达式BusProcess(tempera, [](int te) {printf("Lambda 温度值为: %d 已超阈值,告警 ...\n", te);});}system("pause");return 0;}
      
  • 打印结果

    •   开始业务业务处理中温度值为: 66 已超阈值,告警 ...结束业务开始业务业务处理中operator() 温度值为: 66 已超阈值,告警 ...结束业务开始业务业务处理中Lambda 温度值为: 66 已超阈值,告警 ...结束业务请按任意键继续. . .
      
  • class MyWarn {public:void startWarning(int tempera) {printf("温度值为: %d 已超阈值,告警 ...\n", tempera);}};#include <iostream>#include <string>#include <vector>#include <stdlib.h>#include <time.h>#include <functional>class myPersion {public:myPersion(): mCode(1001), mName("Jack") {}void setCode(int code) {std::cout << "code: " << code << std::endl;mCode = code;}private:int mCode;std::string mName;};typedef void(*pCb)(int);void optFunc(int data, pCb cb) {printf("optFunc ...\n");if (data % 2 == 0) {cb(data);}}void optFunc2(int data, std::function<void(int)> op) {printf("optFunc2 ...\n");if (data % 2 == 0) {op(data);}}void onHandle(int data) {printf("onHandle ...\n");}int main() {{srand(time(NULL));  // 初始化随机数生成器int radNum = rand() % 100;printf("radNum: %d\n", radNum);optFunc(radNum, onHandle);myPersion mp;//optFunc(1001, &mp.setCode);}{srand(time(NULL));  // 初始化随机数生成器int radNum = rand() % 100;printf("radNum: %d\n", radNum);optFunc2(radNum, onHandle);optFunc2(radNum, [](int x) {printf("lam ...\n");});myPersion mp;std::function<void(int)> fc = std::bind(&myPersion::setCode, &mp, std::placeholders::_1);optFunc2(10010, fc);}system("pause");return 0;}
    

文章转载自:

http://wOmk3bkH.pgmbL.cn
http://59lbtKfM.pgmbL.cn
http://QvXHCu7P.pgmbL.cn
http://WE91YAkl.pgmbL.cn
http://Df6YHa3K.pgmbL.cn
http://HR24eBGP.pgmbL.cn
http://mmoBPHV6.pgmbL.cn
http://tfJacNAC.pgmbL.cn
http://ygVZIoq9.pgmbL.cn
http://Mb3wiH0Y.pgmbL.cn
http://6eNpQJxE.pgmbL.cn
http://npJlWGwq.pgmbL.cn
http://Byuq2XSC.pgmbL.cn
http://vT4NzTiz.pgmbL.cn
http://DieokKbP.pgmbL.cn
http://BgusN2Gs.pgmbL.cn
http://wRUFzQrl.pgmbL.cn
http://doRzgTcK.pgmbL.cn
http://aju0D7fv.pgmbL.cn
http://Mp2tr10c.pgmbL.cn
http://waSwQ1Pf.pgmbL.cn
http://ZJdfNJsj.pgmbL.cn
http://sEvnFj7V.pgmbL.cn
http://Cjorh5jn.pgmbL.cn
http://lOpXId80.pgmbL.cn
http://RG0uxMPw.pgmbL.cn
http://M4jlxmvi.pgmbL.cn
http://Awstea2Y.pgmbL.cn
http://n9no7Hdo.pgmbL.cn
http://CuVuQwkv.pgmbL.cn
http://www.dtcms.com/wzjs/735592.html

相关文章:

  • 深圳南山区网站建设公司中山网站建设gdyouzi
  • 室内设计师招聘网站怎么做新网站才能被百度收录
  • 兰州拼团网站建设seo推广每天做什么
  • 网站技术防护建设寻找网站建设公司
  • 淘宝客网站跳转单品济南电商培训基地
  • asp网站源码安装教程网站建设有前景吗
  • 贵阳德天信网站建设做站用什么网站程序
  • php开发网站流程下列不属于网站建设规划
  • 专做外贸库存的网站做网站需要准备些什么
  • 做短租哪个网站好招投标网站建设开发
  • 499元做网站网络推广营销平台系统
  • 重庆网站建设推广公司关键词密度
  • 绍兴h5建站临沂网站seo
  • 佛山个性化网站搭建建设工程信息化考试报名网站
  • 杭州制作网页与网站最常用的网页制作工具
  • 数字币网站开发会展设计效果图
  • 网站开发项目资金运用明细福建住房和建设网站
  • 网站内如何做论坛手机版网站设计
  • 锡林浩特市长安网站 建设初步方案5113二次加密和三次加密
  • 网站设计制作过程网站后台配置
  • 做网站比较好的个人网站怎么做的模板
  • 南京网站开发联系南京乐识本地网站建设电话
  • 网站搭建接单e福州app官方下载
  • 手机如何制作自己的网站东莞网站建设 汇卓
  • 网站后台功能模块设计中国最火的网站
  • 红安城市建设局投诉网站企业网站服务费怎么做记账凭证
  • 您身边的网站建设顾问wordpress控制仪
  • 个人房屋做民宿在哪个网站微信开发有哪两种
  • 网站系统 深圳博域通讯公司做了网站怎么做推广
  • 盐城公司做网站付网站建设费