Sign in

gwj / po_oopc · Files

GitLab

  • Go to dashboard
  • Project
  • Activity
  • Files
  • Commits
  • Network
  • Graphs
  • po_oopc
  • examples09
  • 07-closures
  • fun5.cpp
  • Reformatted code
    c0b14c54
    Grzegorz Jabłoński authored
    2022-09-16 14:36:36 +0200  
    Browse Code »
fun5.cpp 408 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 <algorithm>
#include <functional>
#include <iostream>
#include <vector>

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);
};