Qué significa "[esto] " en C++
Cuando estaba leyendo la API Cocos2dx 3.0
, encontré algo como esto:
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);
}
};
¿Qué significa [this]
? ¿Es esta nueva sintaxis en C++11
?
46
1 answers
¿Qué significa [esto]?
Introduce un lambda - un objeto de función llamable. Poner this
entre paréntesis significa que la lambda captura this
, para que los miembros de este objeto estén disponibles dentro de él. Lambdas también puede capturar variables locales, por valor o referencia, como se describe en la página vinculada.
La lambda tiene una sobrecarga de operator()
, por lo que se puede llamar como una función:
Event * event = some_event();
listener(event);
Que ejecutará el código definido en el cuerpo de la lambda.
¿Es esta nueva sintaxis en C++11?
Sí.
58
Author: Mike Seymour,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-08 07:28:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-08 07:28:41