Created
December 26, 2018 23:08
-
-
Save miladvafaeifard/2aea4493d15a42c19b6b34d1fa57db4d to your computer and use it in GitHub Desktop.
my first lambda use in c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pch.h" // for visual studio usage | |
#include <iostream> | |
#include <functional> | |
class Calculator { | |
public: | |
Calculator(int num) : _baseNum(num) {}; | |
int add(int a, int b) { | |
auto addTwoIntegers = [](int x) -> std::function<int(int)> { | |
return [=](int y) { | |
return x + y; | |
}; | |
}; | |
auto higherOrder = [this](const std::function<int(int)>& fn, int z) { | |
return _baseNum + fn(z); | |
}; | |
return higherOrder(addTwoIntegers(a), b); | |
} | |
private: | |
int _baseNum; | |
}; | |
int main() | |
{ | |
Calculator cal(10); | |
std::cout << "Answer: " << cal.add(5, 5) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment