STD: function, STD: bind Review

Source: Internet
Author: User
# Include <iostream> # include <functional>
// STD: bind returns the function object void fun1 (int A, int B) {STD: cout <A <B <STD: Endl ;} using namespace STD: placeholders; Class A {public: void fun2 (int A, int B) {STD: cout <A <B <STD :: endl ;}; int main () {// first usage, bind the real parameter STD: BIND (fun1, 1, 2 )(); // during the call, copy 1 and 2 is passed as the real parameter to fun1. The bound parameters in the BIND must be the same as the number of parameters called by fun1, the parameter will be passed to fun1 as the input parameter // placeholder _ 1, and the positive parameter STD: BIND (fun1, _ 2, _ 1) will be accepted only when the function is called) (1, 2); // The placeholder name indicates the order in the call type. This indicates fun1 (2, 1) // bind the member function a A; STD :: BIND (& A: fun2, A, _ 1, _ 2) (3, 4 ); // you need to pass something similar to this pointer so that this function object can be called & you must add // functon usage. The most common usage can store the results returned by bind and callback when needed later, is a callback mechanism STD: function <void (INT, INT)> func; // function can store function pointer objects. Previously, it was assumed that only STD: bind func = fun1 can be stored; func (5, 6); // function calls a member function // 1. in the function, write the class name STD: function <void (A &, Int, INT)> fun1; // you can assign a value to int. The Int returns the void member function fun1 = STD:: BIND (& A: fun2, _ 1, _ 2, _ 3); fun1 (A, 8, 9); // 2. write a class instance in the bind. This method is mostly STD: function <void (INT, INT)> fun2; fun2 = STD: BIND (& A: fun2,, _ 1, _ 2); fun2 (9, 10); getchar (); Return 0 ;}

 

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.