m4n71k0r

Untitled

Jun 17th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. struct FirstClass
  7. {
  8.     FirstClass(): x(0)
  9.     {
  10.     }
  11.  
  12.     int get_x() const
  13.     {
  14.         return x;
  15.     }
  16.  
  17.     function<int ()> f1 = [this]() -> int
  18.     {
  19.         cout << "called member function f1..." << endl;
  20.         ++x;
  21.         f1 = f2;
  22.         return 5;
  23.     };
  24.  
  25. private:
  26.  
  27.     function<int ()> f2 = [this]() -> int
  28.     {
  29.         cout << "called member function f2..." << endl;
  30.         return x;
  31.     };
  32.  
  33.     int x;
  34. };
  35.  
  36. int main()
  37. {
  38.     FirstClass m;
  39.     m.f1();
  40.     m.f1();
  41.  
  42.     function<int ()> f3 = []() -> int
  43.     {
  44.         cout << "called free function f3..." << endl;
  45.         return 100500;
  46.     };
  47.  
  48.     m.f1 = f3;
  49.     m.f1();
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment