fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4.  
  5. class Widget {
  6. public:
  7. Widget(): pImpl(std::make_shared<Impl>()),
  8. pc(new CAT){}
  9. ~Widget() {
  10. delete pc; // I know we should put this code to cpp
  11. // I am just want to show the difference behavior
  12. // between raw pointer and smart pointer
  13. // when widget object destruct
  14. }
  15. private:
  16. struct Impl {
  17. std::string name;
  18. Impl(){std::cout<<"Impl"<<std::endl;}
  19. ~Impl(){std::cout<<"~Impl"<<std::endl;}
  20. };
  21. std::shared_ptr<Impl> pImpl; // use smart pointer
  22. struct CAT
  23. {
  24. std::string name;
  25. CAT(){std::cout<<"CAT"<<std::endl;}
  26. ~CAT(){std::cout<<"~CAT"<<std::endl;}
  27. };
  28. CAT *pc; //raw pointer
  29. };
  30.  
  31.  
  32. int main() {
  33. Widget w;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
Impl
CAT
~CAT
~Impl