STD: tr1: Function

Source: Internet
Author: User

In tr1 of C ++ (Technology
Report) contains a function template class and bind template function. They can be used to implement functions similar to function pointers, but they are more flexible than function pointers.
. See Scott Meyers. <valid tive C ++ (3rd edition)>.
Item 35. The following describes how to use it.

1. When pointing to a global function or a static member function

In essence, global functions and static member functions are not partitioned.
No. In terms of usage, except for static member functions that must be prefixed with the domain name classname:, there is no difference. In fact, global functions may also be placed in namespaces, or use
Full-area operator, for example, namespace: function ()
Or: function, which is essentially the same, and the situation is also consistent with that of static member functions, so they are no different. Let's discuss them together.

This situation is relatively simple. You only need to define a type.

# Include <iostream>

# Include <iomanip>

# Include <tr1/memory>

# Include <tr1/functional>

Typedef STD: tr1: function <void (INT)> handlerevent;

Then define a member variable.

Class sharp {

Public:

Handlerevent;

};

Then, you can dynamically load the Event Response Function by setting the value of handlerevent in other functions, such:

Class rectangle {

PRIVATE:

STD: string name;

Sharp sharp;

Public:

Void initial (void );

Const sharp getsharp () const;

Static void onevent (int param ){// --------------- (1)

STD: cout <"invode onevent method, get parameter:" <Param <STD: Endl;

}

};

// Class Implementation Method

Void rectangle: initial (){

Sharp. handlerevent = handlerevent (& rectangle: onevent );// --------------- (2)

STD: cout <"invode initial function! "<STD: Endl;

}

Const sharp rectangle: getsharp () const {

Return sharp;

}

// The following is the test function:

Int main (INT argc, char * argv []) {

STD: cout <"Hi:" <STD: SETW (50) <"Hello world! "<STD: Endl;

Rectangle rectangle;

Rectangle. Initial ();// --------------- (3)

Rectangle. getsharp (). handlerevent (23 );// --------------- (4)

}

// The output result is as follows:

Hi: Hello world!

Invode initial function!

Invode onevent method, get parameter: 23// --------------- (5)

Note
The static member function is used here. If you remove the static member function before the rectangleCodeUnable to work, compilation fails, because the parameters of static and non-static member functions
The number table is different. The non-static function with the same prototype has one more parameter than the static member function, that is, the first parameter This Pointer Points to the object to which the parameter belongs, the first parameter of any non-static member function is this
So if we remove the static before rectangle, the function prototype is equivalent to the following global function:

Void onevent (rectangle * This, INT );

Institute
This does not match the function type declared by handlerevent. compilation will fail. In addition, since the static member function does not have the this pointer, the call at (3) above causes
The handlerevent in the sharp object directs the static method onevent () of rectangle, so that when the call is made through (4), it will be automatically executed at (1 ).
Static function onevent ().

Ii. Use of STD: tr1: BIND () template Functions

The above STD: tr1: function can be used to bind static member functions. However, to bind non-static member functions, You need to bind () as described below () template Function.

First, describe the usage of bind. Its declaration is as follows:

BIND (function FN, T1 T1, T2 T2 ,..., TN Tn );

FN is the function to be called, T1... TN is a function parameter. If no parameter is specified, a placeholder can be used to represent the form parameter.

STD: tr1: placehoders: _ 1,STD: tr1: placehoders: _ 2,...,STD: tr1: placehoders: _ n

In the preceding exampleStaticRemove the function and change it to a non-static member function.ProgramRun properly. modify the definition of rectangle: initial (void):

Void rectangle: initial (){

Sharp. handlerevent = STD: tr1: BIND (& rectangle: onevent, this,STD: tr1: placeholders: _ 1/* Because the onevent function requires a parameter, a placeholder is used */);

STD: cout <"invode initial function! "<STD: Endl;

}

In this way, the function is loaded dynamically. You do not need to modify other test data. The test results are the same.

Iii. Use of virtual member functions

The case for virtual member functions is the same as described in section 2nd above, you can still consider the function effect. If the square class is inherited from rectangle
Rectangle: onevent overload, defines a new square: onevent, rectangle: Initialize function unchanged, still use rectangle: onevent to bind, then call the member object. onevent (), the specific execution rectangle: onevent also
Is square: onevent. Check whether the static type of the object is rectangle or square.

The following is a simple example:

First, modify the initial () method of the preceding rectangle to a virtual function. For example:

Virtual void onevent (int param ){

STD: cout <"invodeRectangle'sOnevent method, get parameter: "<Param <STD: Endl;

}

Then we write another square class to inherit the rectangle class. And override the onevent method. For example:

Class square: Public rectangle {

Public:

Void onevent (int param ){

STD: cout <"invodeSquare'sOnevent method, get parameter: "<Param <STD: Endl;

}

};

Test code:

Int main (INT argc, char * argv []) {

Rectangle rectangle;

Rectangle. Initial ();

Rectangle. getsharp (). handlerevent (23 );

Square square;

Square. Initial ();

Square. getsharp (). handlerevent (33 );

}

The running result is as follows:

Hi: Hello world!

Invode initial function!

Invode rectangle's onevent method, get parameter: 23

Invode initial function!

Invode Square's onevent method, get parameter: 33

In this way, we can see that Sharp will call the corresponding onevent () method for specific objects. The examples of the above programs can be learned by the reader.

[Reprint] http://blog.csdn.net/xiucaijiang/article/details/5999441

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.