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

A Heap Object Lives Until delete

easyPointers

📄 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* p = new Box(1);
13 Box s(2);
14 cout << "before delete" << endl;
15 delete p;
16 cout << "after delete" << endl;
17 return 0;
18}

🎯 Your Answer