fork(5) download
  1. #include <cstdio>
  2. #include <memory>
  3.  
  4.  
  5. struct base
  6. {
  7. base( int i ): i(i)
  8. {
  9. printf("base ctor\n");
  10. }
  11. ~base()
  12. {
  13. printf("base non-virtual dtor\n");
  14. } // non-virtual
  15. int i;
  16. };
  17.  
  18. struct derived : public base
  19. {
  20. char* s;
  21. derived(int i): base(i), s(new char[i] )
  22. {
  23. printf("derived ctor\n");
  24. }
  25. ~derived()
  26. {
  27. printf("derived dtor\n");
  28. delete [] s;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34. printf("Success\n");
  35.  
  36. //raw pointer
  37. printf("RAW-POINTER\n");
  38. {
  39. base* b = new derived(2);
  40.  
  41.  
  42. delete b;
  43. }
  44. printf("---END-RAW-POINTER--\n");
  45.  
  46. //unique-ptr
  47. printf("UNIQUE_PTR\n");
  48. {
  49. std::unique_ptr<base> bu( new derived(3) );
  50. }
  51. printf("--END-UNIQUE_PTR--\n");
  52.  
  53.  
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Success
RAW-POINTER
base ctor
derived ctor
base non-virtual dtor
---END-RAW-POINTER--
UNIQUE_PTR
base ctor
derived ctor
base non-virtual dtor
--END-UNIQUE_PTR--