一个成员函数的简单示例

有时我们希望回调某个类里面的函数,而不是静态函数或全局函数。

这时就会用到成员函数,以下是成员函数的使用示例:

(以下代码在VS2010及更高版本使用,VS2010以下的版本没有std::functiont和std::bind,而是另外的定义)

 1 #include <functional>
 2 // 回调函数定义,三个参数
 3 typedef std::function<bool(int,int,std::string&)> CallFunc;    
4
// 说明:func函数名, _Object指针, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3表示回调函数的三个参数,按函数参数数量而定 5 #define FUNC_CALLBACK_1(func, _Object) std::bind(&func,_Object, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
6 class TestClass 7 { 8 public: 9 bool func(int a, int b, std::string & str){ return true; } 10 11 }; 12 13 void main() 14 { 15 // 定义函数变量 16 CallFunc _func; 17 // 创建类 18 TestClass test; 19 // 赋值函数变量,如果未赋值则默认为空,可以判断{ if (_func == 0 || _func._Empty()){ return "函数为空!";} } 20 _func = FUNC_CALLBACK_1(TestClass::func, &test); 21 }

 

转载于:https://www.cnblogs.com/elephant-x/p/3305262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值