When I was reading the Cocos2dx 3.0
API, I found something like this:
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
?
this
. – Nuttthis
? Wouldn't it automatically be captured by the reference toonKeyPressed
andonKeyReleased
(assuming they're members ofthis
). Also a warning,keyboardEvent
is now holding a copy of thethis
pointer... woe betide you ifkeyboardEvent
manages to outlive whateverthis
is pointing at.... (standard object lifetime issues) – Feneliaauto
keyword and the null pointer literalnullptr
are also C++11. – Yonkers