fork download
  1. #include <memory>
  2. #include <iostream>
  3.  
  4. class A
  5. {
  6. public:
  7. A() { std::cout << "Hi" << std::endl; }
  8. ~A() { std::cout << "Bye" << std::endl; }
  9. };
  10.  
  11. class B
  12. {
  13. public:
  14. B(): a(new A()) {};
  15. //~B() {} <-- destructor is no longer needed, the unique_prt will delete the object for us
  16. private:
  17. std::unique_ptr<A> a;
  18. };
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22. B b;
  23. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
Hi
Bye