When I was reading the Cocos2dx 3.0
API, I found something like this:
当我阅读Cocos2dx 3.0 API时,我发现了如下内容:
auto listener = [this](Event* event){
auto keyboardEvent = static_cast<EventKeyboard*>(event);
if (keyboardEvent->_isPressed)
{
if (onKeyPressed != nullptr)
onKeyPressed(keyboardEvent->_keyCode, event);
}
else
{
if (onKeyReleased != nullptr)
onKeyReleased(keyboardEvent->_keyCode, event);
}
};
What does [this]
mean? Is this new syntax in C++11
?
(这)是什么意思?这是c++ 11中的新语法吗?
1 个解决方案
#1
58
What does [this] means?
[这]意味着什么?
It introduces a lambda - a callable function object. Putting this
in the brackets means that the lambda captures this
, so that members of this object are available within it. Lambdas can also capture local variables, by value or reference, as described in the linked page.
它引入一个lambda -一个可调用的函数对象。把它放在括号中意味着lambda捕获到这个对象,因此该对象的成员可以在其中使用。Lambdas还可以根据值或引用捕获局部变量,如在链接页面中描述的那样。
The lambda has an overload of operator()
, so that it can be called like a function:
lambda具有操作符()的重载,因此可以像函数一样调用:
Event * event = some_event();
listener(event);
which will run the code defined in the body of the lambda.
它将运行lambda中定义的代码。
Is this new syntax in C++11?
这是c++ 11中的新语法吗?
Yes.
是的。
#1
58
What does [this] means?
[这]意味着什么?
It introduces a lambda - a callable function object. Putting this
in the brackets means that the lambda captures this
, so that members of this object are available within it. Lambdas can also capture local variables, by value or reference, as described in the linked page.
它引入一个lambda -一个可调用的函数对象。把它放在括号中意味着lambda捕获到这个对象,因此该对象的成员可以在其中使用。Lambdas还可以根据值或引用捕获局部变量,如在链接页面中描述的那样。
The lambda has an overload of operator()
, so that it can be called like a function:
lambda具有操作符()的重载,因此可以像函数一样调用:
Event * event = some_event();
listener(event);
which will run the code defined in the body of the lambda.
它将运行lambda中定义的代码。
Is this new syntax in C++11?
这是c++ 11中的新语法吗?
Yes.
是的。