fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class Building {
  5. // C++ classes are private by default
  6. int timer_;
  7. std::function<void(Building*)> update_;
  8.  
  9. public:
  10. void update() {
  11. update_(this);
  12. }
  13.  
  14. static Building getBuildingA() {
  15. Building b;
  16. b.timer_ = 42;
  17. b.update_ = [](Building* b) {
  18. b->timer_++;
  19. std::cout << b->timer_ << '\n';
  20. };
  21. }
  22. };
  23.  
  24. int main() {
  25. Building b = Building::getBuildingA();
  26. b.update();
  27. return 0;
  28. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
43