Advertisement
Guest User

Untitled

a guest
Aug 19th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class CallBase {
  4. public:
  5.     virtual ~CallBase () {}
  6.  
  7.     virtual void call () = 0;
  8. };
  9.  
  10. template<typename F, typename A>
  11. class Call : public CallBase {
  12. private:
  13.     F func;
  14.     A args;
  15.  
  16. public:
  17.     Call (F func, A args) : func(func), args(args) {}
  18.  
  19.     ~Call () {}
  20.  
  21.     void call () {
  22.         func(args);
  23.     }
  24. };
  25.  
  26. class Test {
  27. private:
  28.     CallBase* call;
  29.  
  30. public:
  31.     template<typename F, typename... A>
  32.     Test (F func, A... args) {
  33.         this->call = new Call<F, A...>(func, args...);
  34.     }
  35.  
  36.     ~Test () {
  37.         delete call;
  38.     }
  39.  
  40.     void callFunction () {
  41.         call->call();
  42.     }
  43. };
  44.  
  45. void f (int a) {
  46.     std::cout << a << std::endl;
  47. }
  48.  
  49. int main () {
  50.     Test test(f, 2);
  51.  
  52.     test.callFunction();
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement