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

A Temporary Dies at the Semicolon

mediumClasses

📄 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 int id() const { return m_id; }
10};
11 
12int main() {
13 cout << "start" << endl;
14 cout << Box(7).id() << endl;
15 cout << "middle" << endl;
16 Box(8);
17 cout << "end" << endl;
18 return 0;
19}

🎯 Your Answer