mercredi 28 janvier 2015

Confused by variable scope - destructor called unexpectedly

I have 3 classes, Fruit, Apple and Orange, with Fruit being the parent of both. I have a static method where I do the following:



int32_t Fruit::frutificate(const Settings& settings) {
Fruit listener;
if (settings.has_domain_socket()) {
listener = Apple(settings);
} else {
listener = Orange(settings);
}
return uv_run(listener.loop, UV_RUN_DEFAULT);
}


What confuses me is that the destructor of Apple, which runs clean up code Fruit doesn't have, is called within the condition. Apple is a child of Fruit and Fruit is declared outside the condition so shouldn't the scope last until the return?


Aucun commentaire:

Enregistrer un commentaire