notes above destructor order

This commit is contained in:
Jidong Xiao
2025-01-17 12:05:01 -05:00
committed by JamesFlare1212
parent 237db36f55
commit 944a2129a6

View File

@@ -211,21 +211,23 @@ A **destructor** is a special member function automatically called when an objec
```cpp
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() { cout << "Constructor called\n"; }
~MyClass() { cout << "Destructor called\n"; }
MyClass() { std::cout << "Constructor called" << std::endl; }
~MyClass() { std::cout << "Destructor called" << std::endl; }
};
int main() {
MyClass obj; // Constructor called
MyClass obj1; // Constructor called
MyClass obj2; // Constructor called
// Destructor will be called automatically at the end of scope
return 0;
}
```
**Question**: in the above example, obj1's destructor or obj2's destructor, which one gets called first?
## 4.4 Overloading `operator<<` in C++
### Purpose