Sign in

gwj / po_oopc · Files

GitLab

  • Go to dashboard
  • Project
  • Activity
  • Files
  • Commits
  • Network
  • Graphs
  • po_oopc
  • examples09
  • 06-closures
  • fun5.cpp
  • Added foreach example
    8e894b70
    Grzegorz Jabłoński authored
    2019-12-11 18:48:43 +0100  
    Browse Code »
fun5.cpp 381 Bytes
Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class C
{
  int c;
public:
  C(int _c): c(_c) {}; 
  
  auto fun()
  {
    return [this](int x) { return c + x;};
  }

  void print(function<int(int)>f)
  {
      cout << fun()(3) << endl;
  }
};
  
int main ()
{
  C c1(1);
  C c2(2);
  
  auto f = c2.fun();
  c1.print(f);
};