- #1
yungman
- 5,641
- 227
I am stepping through the program and I actually wrote down the steps in comment each step the debugger step through. I have a few question I still don't understand. Here is the program:
Took me a while to line up the step numbers in straight column so it's easier for you guys to read.
This is the printout with steps labeled:
1) I understand in the first 20 steps, object trouble, moreTrouble and bigTrouble are created. Each inherited the member funtion getM() in line 12 and message in line 8. These are all in the objects already. How come step 25 has to jump to step 26 to access getM() in line 12? this is the same from step 41 jump to step 42. Does the objects have their own getM(), that the program doesn't have to jump to the Trouble class anymore?
2) If you look at the printout with the steps, notice Destructor is called between step 43 and 44. same as from step 54 to 55. I don't see it stepping through the destructor but it said they are executed.
3) Following from question 2 above, you can see destructors are being called again in steps 59, 60 and 61. The program calls the destructors for each object TWICE. Why?
Thanks
C++:
//Matching catch handler with exception
#include<iostream>
#include<string>
#include<string_view>
#include<cstring>
using namespace std;
class Trouble {public:
string message;
Trouble(string_view str = " There is a problem") // 1 7 16
{message = str;} // 2 8 17
virtual ~Trouble()=default;//Base classes must have virtual des.
virtual string_view getM() const { return message; }}; // 26 42 53 61
class MoreTrouble : public Trouble
{public: MoreTrouble(string_view str=" Theres more trouble")// 5 14
: Trouble(str) {} // 6 9 15 18
~MoreTrouble() { cout<<" Destructor MoreTrouble.\n\n";}}; // 60 62
class BigTrouble : public MoreTrouble
{ public: BigTrouble(string_view str=" Really big trouble")// 12
: MoreTrouble(str) {}; // 13 19
~BigTrouble() { cout<<" Destructor BigTrouble.\n\n";}};// 59 63
int main()
{ Trouble trouble; // Start 3
MoreTrouble moreTrouble; // 4 10
BigTrouble bigTrouble; // 11 20
for (int i = 3; i < 7; ++i) // 21 30 36 46 57
{ try // 22 31 37 47
{if (i == 3) throw trouble; // 23 38 48
else if (i == 5) throw moreTrouble; // 32 39 49
else if (i == 6) throw bigTrouble; // 33 50
} // 51
catch (const BigTrouble& t)
{cout<<" BigTrouble caught: "<<t.getM()<<"\n\n";} // 40 52 54
catch (const MoreTrouble& t)
{cout<<" MoreTrouble caught: "<<t.getM()<<"\n\n";} // 24 41 43
catch (const Trouble& t)
{cout<<" Trouble caught: "<<t.getM()<<"\n\n";} // 25 27
cout<<" End of for loop(after catch blocks), i = "<<i<<"\n\n";// 28 34 44 55
} // 29 35 45 56
}// // 58 64
Took me a while to line up the step numbers in straight column so it's easier for you guys to read.
This is the printout with steps labeled:
1) I understand in the first 20 steps, object trouble, moreTrouble and bigTrouble are created. Each inherited the member funtion getM() in line 12 and message in line 8. These are all in the objects already. How come step 25 has to jump to step 26 to access getM() in line 12? this is the same from step 41 jump to step 42. Does the objects have their own getM(), that the program doesn't have to jump to the Trouble class anymore?
2) If you look at the printout with the steps, notice Destructor is called between step 43 and 44. same as from step 54 to 55. I don't see it stepping through the destructor but it said they are executed.
3) Following from question 2 above, you can see destructors are being called again in steps 59, 60 and 61. The program calls the destructors for each object TWICE. Why?
Thanks