Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <exception>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Foo {
  9.     public:
  10.     virtual void print() = 0;
  11. };
  12.  
  13. class Bar : public Foo {
  14.     public:
  15.     void print() {
  16.         std::cout << "I'm bar!\n";
  17.     }
  18. };
  19.  
  20. template <typename T>
  21. std::function<void(Foo*)> make_wrapper(std::function<void(T)> fct) {
  22.     return [fct](Foo* event) {
  23.         T* x = dynamic_cast<T*>(event);
  24.         fct(*x);
  25.     };
  26. }
  27.  
  28. int main(void) {
  29.     Bar x = Bar();
  30.     Foo* y = &x;
  31.  
  32.     make_wrapper<Bar>([](Bar z){z.print();})(y);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement