fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. class ExampleImpl {
  5. public:
  6. ExampleImpl() {
  7. std::cout << "constructor called" << std::endl;
  8. }
  9. ~ExampleImpl() {
  10. std::cout << "destructor called" << std::endl;
  11. }
  12. };
  13.  
  14. class Example {
  15. std::shared_ptr<ExampleImpl> impl;
  16. public:
  17. Example() : impl(std::make_shared<ExampleImpl>()){}
  18. Example* operator&() {
  19. return new Example(*this);
  20. }
  21. };
  22.  
  23. int main() {
  24. Example* example_pointer;
  25.  
  26. {
  27. Example example; // prints "constructor called"
  28. example_pointer = &example;
  29. } // example still exists here, nothing is printed
  30. std::cout << "Still exists here\n";
  31.  
  32. delete example_pointer; // deleting automatic variable manually
  33. // Prints "destructor called"
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
constructor called
Still exists here
destructor called