Created
February 29, 2024 03:26
-
-
Save ytxmobile98/f5467302dd80fa5dbb4c9f72915c60e2 to your computer and use it in GitHub Desktop.
C++ member destructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example program | |
| #include <iostream> | |
| #include <memory> | |
| #include <string> | |
| using std::cout; | |
| using std::endl; | |
| using std::make_unique; | |
| using std::unique_ptr; | |
| class Inner { | |
| public: | |
| ~Inner() { | |
| cout << "~Inner()" << endl; | |
| } | |
| }; | |
| class Outer { | |
| private: | |
| Inner inner; | |
| public: | |
| unique_ptr<Inner> innerPtr; | |
| Outer(): innerPtr(make_unique<Inner>()) {} | |
| ~Outer() { | |
| cout << "~Outer()" << endl; | |
| } | |
| }; | |
| int main() { | |
| Outer outer; | |
| std::cout << (outer.innerPtr.get() != nullptr) << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment