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 ```cpp
#include <iostream> #include <iostream>
using namespace std;
class MyClass { class MyClass {
public: public:
MyClass() { cout << "Constructor called\n"; } MyClass() { std::cout << "Constructor called" << std::endl; }
~MyClass() { cout << "Destructor called\n"; } ~MyClass() { std::cout << "Destructor called" << std::endl; }
}; };
int main() { int main() {
MyClass obj; // Constructor called MyClass obj1; // Constructor called
MyClass obj2; // Constructor called
// Destructor will be called automatically at the end of scope // Destructor will be called automatically at the end of scope
return 0; 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++ ## 4.4 Overloading `operator<<` in C++
### Purpose ### Purpose