fork(1) download
  1. #include <iostream> // cout, endl
  2. #include <memory> // shared_ptr
  3. #include <string> // string
  4.  
  5. struct Base
  6. {
  7. virtual std::string GetName() const = 0;
  8. ~Base() { std::cout << "~Base\n"; }
  9. };
  10.  
  11. struct Concrete : public Base
  12. {
  13. std::string GetName() const
  14. {
  15. return "Concrete";
  16. }
  17. ~Concrete() { std::cout << "~Concrete\n"; }
  18. };
  19.  
  20. int main()
  21. {
  22. {
  23. std::cout << "test 1\n";
  24. std::shared_ptr<Base> b(new Concrete);
  25. std::cout << b->GetName() << std::endl;
  26. }
  27.  
  28. {
  29. std::cout << "test 2\n";
  30. Base* pBase = new Concrete;
  31. std::shared_ptr<Base> b(pBase);
  32. std::cout << b->GetName() << std::endl;
  33. }
  34.  
  35. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
test 1
Concrete
~Concrete
~Base
test 2
Concrete
~Base