fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class Bang
  5. {
  6. public:
  7. Bang(int i = 0) : m_val(i)
  8. {
  9. m_foo = [bang = *this] { std::cout << bang.m_val << std::endl; };
  10. }
  11.  
  12. ~Bang()
  13. {
  14. m_val = -1;
  15. }
  16.  
  17. void Foo()
  18. {
  19. m_foo();
  20. }
  21. private:
  22. int m_val;
  23. std::function<void()> m_foo;
  24. };
  25.  
  26. Bang GetBang()
  27. {
  28. return Bang(100500);
  29. }
  30.  
  31. int main()
  32. {
  33. Bang b;
  34. b = GetBang();
  35. b.Foo();
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 4336KB
stdin
Standard input is empty
stdout
100500