Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <functional>
  4.  
  5. class Thing {
  6. public:
  7. void move( float x ) {
  8. std::cout << "moving with " << x << std::endl;
  9. }
  10. };
  11.  
  12. class Other {
  13. public:
  14. using CallbackFunction = std::function<void(float)>;
  15. public:
  16. void go() {
  17. mCallback( 42 );
  18. }
  19.  
  20. void setCallback( CallbackFunction func ) {
  21. mCallback = func;
  22. }
  23.  
  24. protected:
  25. std::function<void(float)> mCallback;
  26. };
  27.  
  28. int main() {
  29. using namespace std;
  30.  
  31. auto thing = make_shared<Thing>();
  32. auto other = make_shared<Other>();
  33.  
  34. other->setCallback( std::bind( &Thing::move, thing.get(), std::placeholders::_1 ) );
  35.  
  36. other->go();
  37.  
  38. return 0;
  39. }
Add Comment
Please, Sign In to add comment