fork download
  1. #include <functional>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct Holder {
  6.  
  7. void init() {
  8. callback = [=]() { cout << "In the callback, value == " << value << endl; };
  9. }
  10.  
  11. void speak() {
  12. cout << "In speak(), value == " << value << endl;
  13. }
  14.  
  15. function<void()> callback;
  16. string value;
  17. };
  18.  
  19. int main() {
  20. Holder h;
  21. h.value = "initial";
  22. h.init();
  23. h.value = "updated";
  24. h.callback();
  25. h.speak();
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
In the callback, value == updated
In speak(), value == updated