CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #57

Destructor Order With a Nested Block

easyClasses

📄 Code

Read carefully — what does this print?
1#include <iostream>
2using namespace std;
3 
4class Box {
5 int m_id;
6public:
7 Box(int id) : m_id(id) { cout << "Create " << m_id << endl; }
8 ~Box() { cout << "Destroy " << m_id << endl; }
9};
10 
11int main() {
12 Box a(1);
13 {
14 Box b(2);
15 cout << "inside" << endl;
16 }
17 Box c(3);
18 cout << "end" << endl;
19 return 0;
20}

🎯 Your Answer