// Another simple example of shared_ptr (not directly related to the previous examples). #include #include #include #include "point.h" using namespace std; int f(){ shared_ptr pp1{new Point{1,2}}, pp2{new Point{3,4}}, pp3; cout << *pp1 << endl; // (1,2) cout << *pp2 << endl; // (3,4) pp1.reset(); // empty pp1 - delete underlying point (1,2) from the heap. pp3 = pp2; // both pp3 and pp2 refer to (3,4). pp3->displace(1,1); // displace point pointed to by pp3 cout << *pp2 << endl; // (4,5) - Point pointed to by pp2 has moved! cout << "Exit f" << endl; // Delete the other point (3,4) from the heap. // Thus both points have disappeared from the heap at this time. } int main(){ f(); cout << "After f()" << endl; // All points on the heap have been released at this point. }