Guest User

Untitled

a guest
Dec 9th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct LogicImpl1
  5. {
  6.     int some_result()
  7.     {
  8.         return 10;
  9.     }
  10. };
  11.  
  12. struct LogicImpl2
  13. {
  14.     std::string some_result()
  15.     {
  16.         return "ten";
  17.     }
  18. };
  19.  
  20. struct Test
  21. {
  22.     template<typename Logic>
  23.     void set_logic(Logic logic)
  24.     {
  25.         logic_functor = [logic = std::move(logic)]() mutable
  26.         {
  27.             std::cout << logic.some_result() << std::endl;
  28.         };
  29.     }
  30.  
  31.     void imagine_async_action()
  32.     {
  33.         logic_functor();
  34.     }
  35.  
  36.     std::function<void()> logic_functor;
  37. };
  38.  
  39. int main()
  40. {
  41.     Test t;
  42.  
  43.     t.set_logic(LogicImpl1{});
  44.     t.imagine_async_action();
  45.  
  46.     t.set_logic(LogicImpl2{});
  47.     t.imagine_async_action();
  48.  
  49.     t.set_logic(LogicImpl1{});
  50.     t.imagine_async_action();
  51.  
  52.     return 0;
  53. }
  54.  
Add Comment
Please, Sign In to add comment