impulsar puntero compartido NULL
Estoy usando reset()
como un valor predeterminado para mi shared_pointer (equivalente a un NULL
).
Pero ¿cómo puedo comprobar si el shared_pointer es NULL
?
¿Devolverá esto el valor correcto ?
boost::shared_ptr<Blah> blah;
blah.reset()
if (blah == NULL)
{
//Does this check if the object was reset() ?
}
4 answers
Uso:
if (!blah)
{
//This checks if the object was reset() or never initialized
}
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
2011-04-10 08:12:56
if blah == NULL
funcionará bien. Algunas personas lo preferirían a probar como bool (if !blah
) porque es más explícito. Otros prefieren este último porque es más corto.
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
2011-04-10 14:45:28
Puede probar el puntero como un booleano: evaluará a true
si no es nulo y false
si es nulo:
if (!blah)
boost::shared_ptr
y std::tr1::shared_ptr
ambos implementan el modismo safe-bool y el std::shared_ptr
de C++0x implementa un operador de conversión explícito bool
. Estos permiten que un shared_ptr
se use como booleano en ciertas circunstancias, de manera similar a como los punteros ordinarios se pueden usar como booleanos.
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
2011-04-10 08:18:49
Como se muestra en la documentación de boost::shared_ptr<>
, existe un operador de conversión booleano:
explicit operator bool() const noexcept;
// or pre-C++11:
operator unspecified-bool-type() const; // never throws
Así que simplemente use el shared_ptr<>
como si fuera un bool
:
if (!blah) {
// this has the semantics you want
}
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
2018-06-20 23:24:10