Skip to content

Instantly share code, notes, and snippets.

@ytxmobile98
Created February 29, 2024 03:26
Show Gist options
  • Select an option

  • Save ytxmobile98/f5467302dd80fa5dbb4c9f72915c60e2 to your computer and use it in GitHub Desktop.

Select an option

Save ytxmobile98/f5467302dd80fa5dbb4c9f72915c60e2 to your computer and use it in GitHub Desktop.
C++ member destructor
// 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