fork(1) download
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4.  
  5. class Foo {
  6. public:
  7. Foo(const std::string& i_name) : name(i_name)
  8. {}
  9.  
  10. std::function<void()> GetPrinter() {
  11. return [this]() {
  12. std::cout << name << std::endl;
  13. };
  14. }
  15.  
  16. std::string name;
  17. };
  18.  
  19. int main() {
  20. std::function<void()> f;
  21.  
  22. {
  23. auto foo = std::make_shared<Foo>("ok");
  24. f = foo->GetPrinter();
  25. }
  26.  
  27. auto foo = std::make_shared<Foo>("WRONG");
  28.  
  29. f();
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
WRONG