**unique_ptr** Only one pointer is granted the ownership of one memory space. If the ownership needs to be changed,use std::move semantic to transfer the ownership. ```c++ void print_unique_ptr(const std::unique_ptr<int>& sptr) { std::cout << "value: " << *sptr << std::endl; std::cout << "addr: " << sptr.get() << std::endl; std::cout << "---------------------\n"; } int main() { std::unique_ptr<int> sp = std::make_unique<int>(1); print_unique_ptr(sp); *sp = 2; print_unique_ptr(sp); sp.reset(); sp = std::make_unique<int>(3); print_unique_ptr(sp); // a new pointer points to the same memory std::unique_ptr<int> another_ptr = std::move(sp); std::cout << "-----\n"; std::cout << sp.get() << std::endl; std::cout << "-----\n"; print_unique_ptr(another_ptr); }
**weak_ptr** weak_ptr is similar to shared_ptr, except that it does not hold Reference Counter. ```c++ class Rectangle { int width; int height; public: Rectangle(int w, int h) : width(w), height(h) {} int area() { return width * height; } }; int main() { shared_ptr<Rectangle> P1(new Rectangle(10, 5)); // create weak ptr weak_ptr<Rectangle> P2 (P1); // This'll print 50 cout << P1->area() << endl; // This'll print 1 as Reference Counter is 1 cout << P1.use_count() << endl; return 0; }