• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

在C++中使用tr1实现functor/函数指针/成员函数指针

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.需要头文件#include <functional>

2.定义functor变量 :

std::tr1::function< T* (P1*, P2*) > DpdCreateT;

BCB可以先typedef一下函数声明

typedef T* (Delegate)(P1*, P2*);

std::tr1::function< Delegate > DpdCreateT;

 

3.连接:

类函数

xx.DpdCreateT = std::tr1::bind(

  & ZZZ::CreateConnection,  //类函数地址

  & instance, // 对象实例地址 

  std::tr1::placeholders::_1, // 参数1占位符 

  std::tr1::placeholders::_2 // 参数1占位符

);

全局函数,直接赋值即可

xx.DpdCreateT = GlobalCreateFunction;

 

 //------------------------------------------------------------------------------

简单函数指针

typedef void (*FooPtr)(int, double);

void Foo(int anInt, double aDouble)
{
 std::cout<<"Foo() = "<<anInt<<", "<<aDouble<<endl;  
}

FooPtr func = &Foo;
(*func)( 1, 2.0 );

 //------------------------------------------------------------------------------

成员函数指针

typedef int (SomeClass::*MemberFooPtr)(int, double);


MemberFooPtr p;

SomeClass sc;

p = &SomeClass::Foo;
(sc.*p)(1, 2); 

 

//-------------------------------------------------------------------------------

VS 2008中

#include <functional>

定义:

typedef void (SetFrameValueActionDelegate)(T*, V frameValue);
std::tr1::function<SetFrameValueActionDelegate> SetFrameValueAction;

 

绑定:

mWeekViewGroupLocationAnimation.SetFrameValueAction
 = std::tr1::bind( &MyClass::mWeekViewGroup_LocationAnimation_SetFrameValue,
      &mRenderGroupWeekView,
      std::tr1::placeholders::_2);


 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#几个经常用到的字符串的截取发布时间:2022-07-13
下一篇:
C++抽象类发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap