在C++中使用tr1實現functor/函數指標/成員函數指標

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.