Use tr1 in C ++ to implement functor/function pointer/member function pointer

Source: Internet
Author: User

1. header file # include <functional>

2. Define the functor variable:

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

BCB can use typedef to declare a function.

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

Std: tr1: function <Delegate> DpdCreateT;

 

3. Connection:

Class Functions

Xx. DpdCreateT = std: tr1: bind (

& ZZZ: CreateConnection, // class function address

& Instance, // object instance address

Std: tr1: placeholders: _ 1, // placeholder for parameter 1

Std: tr1: placeholders: _ 2 // placeholder for parameter 1

);

Global function. assign a value directly.

Xx. DpdCreateT = GlobalCreateFunction;

 

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

Simple function pointer

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

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

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

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

Member function pointer

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

MemberFooPtr p;

SomeClass SC;

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

 

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

In VS 2008

# Include <functional>

Definition:

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

 

Binding:

MWeekViewGroupLocationAnimation. SetFrameValueAction
=Std: tr1: bind(& MyClass: mWeekViewGroup_LocationAnimation_SetFrameValue,
& MRenderGroupWeekView,
Std: tr1: placeholders: _ 2);

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.