fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class Human
  5. {
  6. std::function<void()> mFunc;
  7. public:
  8. void setMyFunction(std::function<void()> func) { mFunc = func; }
  9. void callMyFunction() { if (mFunc) mFunc(); }
  10. };
  11.  
  12. void someRandomFunction() { std::cout << "Hello world\n"; }
  13.  
  14. int main()
  15. {
  16. Human lisa;
  17.  
  18. lisa.setMyFunction(&someRandomFunction);
  19. lisa.callMyFunction();
  20. }
  21.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Hello world