fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class foo;
  5. foo *f;
  6.  
  7. class foo
  8. {
  9. std::function<void()> bar;
  10.  
  11. public:
  12. foo(std::function<void()> bar)
  13. : bar(bar)
  14. {
  15. f = this;
  16. }
  17.  
  18. void run()
  19. {
  20. bar();
  21. }
  22. };
  23.  
  24. int &func()
  25. {
  26. int *i = new int;
  27. std::cout << i << std::endl;
  28. new foo
  29. ([=]
  30. {
  31. *i = 42;
  32. std::cout << i << std::endl;
  33. });
  34. return *i;
  35. }
  36.  
  37. int main()
  38. {
  39. int &i = func();
  40. f->run();
  41. std::cout << &i << std::endl;
  42. return 0;
  43. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0x8d95008
0x8d95008
0x8d95008