#include <iostream> using namespace std; class Test { static int count; int id; public: Test() { count++; id = count; cout << "Constructing object number " << id << endl; if(id == 4) throw 4; } ~Test() { cout << "Destructing object number " << id << endl; } }; int Test::count = 0; int main() { try { Test array[5]; } catch(int i) { cout << "Caught " << i << endl; } }
(A)
Constructing object number 1 Constructing object number 2 Constructing object number 3 Constructing object number 4 Destructing object number 1 Destructing object number 2 Destructing object number 3 Destructing object number 4 Caught 4
(B)
Constructing object number 1 Constructing object number 2 Constructing object number 3 Constructing object number 4 Destructing object number 3 Destructing object number 2 Destructing object number 1 Caught 4
(C)
Constructing object number 1 Constructing object number 2 Constructing object number 3 Constructing object number 4 Destructing object number 4 Destructing object number 3 Destructing object number 2 Destructing object number 1 Caught 4
(D)
Constructing object number 1 Constructing object number 2 Constructing object number 3 Constructing object number 4 Destructing object number 1 Destructing object number 2 Destructing object number 3 Caught 4
Respuesta: (B)
Explicación: Los destructores se llaman en orden inverso a los constructores. Además, después del bloque try, los destructores se llaman solo para objetos completamente construidos.
Cuestionario de esta pregunta
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA