Tr1::function of C + +

Source: Internet
Author: User
Tags types of functions

Introduction of Tr1::function

It is a class template, and the member variable of a class is a function pointer. It can be thought of as an intelligent function pointer (compared with the shared_ptr smart pointer).

I. Header file

#include <functional>

Two. Use

T.cpp#include "stdafx.h" #include <iostream> #include <functional>using namespace std;void foo (int i) { cout<< "AAA" <<ENDL;} void (*p) (int) =foo;int _tmain (int argc, _tchar* argv[]) {       function<void (int) > FM;       Fm=foo;       (*p) (2);       FM (2);       return 0;}

You can see that it looks like a function pointer, but it has a lot of things that a function pointer can't do. Let's talk slowly.

1. Define a Function object.

Because function is a class template, it is used to define the object of a class first.

function <void (int) > FM; the first parameter in-----<> is the return type of the function to bind, and the second argument is the argument list of the function to bind. Note use parentheses around.

2. Like a function pointer, this pointer needs to point to a function.

fm=function<void (int) > (foo);

3. Because the class template function overloads the () symbol. So it's more easy to use than a function pointer.

Fm (2);

Here's why we have tr1::function. That is, its advantages over function pointers:

1. Types of functions that are bound

function pointers can only be bound to normal external functions. Instead, tr1::function can bind various function types.

(1) Static functions of external common functions and classes

T.cpp#include "stdafx.h" #include <iostream> #include <functional>using namespace Std;class a{public: static void foo (inti) {cout<< "AAA" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {       function<void (int) >fm (a::foo);       function<void (int) > fm;fm=function<void (int) > (a::foo); Also OK       void (*p) (inti) =a::foo ();//error       FM (2);       return 0;}

Because external functions are similar to static classes, they are very similar in use.

(2) The non-static member function of the class.

T.cpp#include "stdafx.h" #include <iostream> #include <functional>using namespace Std;class a{public: void foo (int i) {cout<< "AAA" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {       A B;       function<void (int) >fm=bind (&a::foo,b,tr1::p laceholders::_1);//ok       //function<void (int) > fm= B.foo ();//error       FM (2);       return 0;}

Note that must be &a::foo (), this symbol & can not be less, this is determined by the function. The Foo in bind just takes a parameter, and actually requires two arguments, because the static function exists without an object, and not the static member function must have an object before it exists, so this member function needs to indicate which object is the member function.

(3) Bind virtual function, render polymorphic

T.cpp#include "stdafx.h" #include <iostream> #include <functional>using namespace Std;class a{public:  virtual void foo (int i) {cout<< "A" <<ENDL;}  void Fun () {       function<void (int) >fm=bind (&a::foo,this,tr1::p laceholders::_1);       FM (2);//here and Directly call Foo (); the effect is the same. does not change its polymorphic nature}};class b:public a{public:void foo (inti) {cout<< "B" <<endl;}}; int _tmain (int argc, _tchar* argv[]) {       b b;       B.fun ();       return 0;}

In fact, this is not the special nature of the function, but the function is an ordinary class, not because it changes the polymorphic nature. Here and directly call Foo (), the same effect.

2. Parameters in the constructor (1) The first is the function name, which has been said above

(2) can be a function object! (A function object is a class that overloads the Operator "()" so that the object of the class can be: A (...); It's like a function, so it's called a function object.

T.cpp#include "stdafx.h" #include <iostream> #include <functional>using namespace Std;class a{public:       void operator () (int i) {cout<< "A" <<endl;       }       void foo () {}};int _tmain (int argc, _tchar* argv[]) {       a A;       function<void (int) > FM (a);       FM (2);       return 0;}

Note here, actually can put a class object into function! This seems to violate the intent of the function pointer. But note: Because function does not provide functions that return what it has, it is only FM (2), which invokes the overloaded () function in the class. You cannot call other functions in a class. So it's still a function pointer, but the function that the pointer points to is the overloaded () function in a class.

3. Several other member functions of the Function class template

(1) Assign function, assigning a function entity to this function pointer.

(2) Swap function, exchange two function pointers have something

(3) The target function, which tests whether the function type pointed to by this function pointer is not the specified type

(4) Target_type function, gets the type information of the function pointer

Not commonly used, also not good use, so understand can.

Summary: In fact, functions and function pointers are very similar, just more than the function pointer can point to some special functions.

Normal function pointers can only point to normal external functions

Functions can point to: external functions, static functions of classes, non-static functions of classes, virtual functions of classes, objects of classes (function objects).

Tr1::function of C + +

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.