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

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);

 

原文链接: https://www.cnblogs.com/mrfangzheng/archive/2010/06/28/1766707.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/12237

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 上午11:10
下一篇 2023年2月7日 上午11:11

相关推荐