Cocos2d-x callback function Lambda expression

Source: Internet
Author: User

Cocos2d-x callback function Lambda expression

 

There are a lot of Cocos2d-x source code

Auto item2 = MenuItemFont: create (--- Go Back ---, [&] (Ref * sender ){
Static_cast (_ Parent)-> switchTo (0 );
});

 

Auto item3 = MenuItemSprite: create (spriteNormal, spriteSelected, spriteDisabled, [] (Ref * sender ){
Log (sprite clicked !);
});

 

This type of code.

From the function declaration of the create FUNCTION, we can see that the Code [] and [&] are located in the place where the callback function is located.

In fact, this is a new syntax introduced by C ++ 11, called Lambda expressions.

In C ++ 98, our callback function must be set in this way,

 

boolcompare( int &a, int & B) // define the comparison function first { returna>b; // Sort in descending order }Sort (a, a + n, compare); // call

 

 

Now we have added the Lambda syntax standard:

We can

Sort (a, a + n, [] (int a, int B) {return a> B ;});

Is it much more concise? I have read the benefits of Lambda. The following describes Lambda in detail.

In C ++, a lambda expression represents a callable code unit. We can understand it as an unnamed inline function. Unlike normal functions, lambda must use the tail return function to specify the return type.

The Lambda type is unique and the corresponding object cannot be explicitly declared through the type name, but the auto keyword and type derivation can be used:

Auto f = [] (inta, intb) {returna> B ;});

We noticed that there is a [] in the front of the Lambda expression that sometimes exists and sometimes does not exist. What is the problem?

 

[] // No variable is specified. Using a variable without definition will cause temporary changes.

[X, & y] // x is set as values, and y is set as values.

 

[&] // Any external changes used are implicitly referenced in the form of an exam. [=] // Any external changes used are implicitly referenced in the volume value method. [&, X] // x is implicitly referenced as a forward value. The number of changes is referenced in the examination. [=, & Z] // use the explain method to reference it. The variable value is referenced as the variable value.
We noticed that external variables are referenced in all expressions. On the contrary, no external variables are referenced if no external variables are referenced &. 

 

In the new C ++ 14 standard, generic expressions based on type inference are also supported.

sort(a,a+n,[](const auto&a,const auto&b){return a>b;});// Sort in descending order: not dependent on the specific types of a and B

 

Benefits:

Because parameter types can be deduced like function template parameters without coupling with specific parameter types, it is helpful to refactor the code. Similar to the function of declaring variables using auto, it also allows you to avoid writing too complex parameter types. In particular, you do not need to explicitly specify the parameter type to make it easier to use higher-order functions.

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.