Skip to content

Instantly share code, notes, and snippets.

@miladvafaeifard
Created December 26, 2018 23:08
Show Gist options
  • Save miladvafaeifard/2aea4493d15a42c19b6b34d1fa57db4d to your computer and use it in GitHub Desktop.
Save miladvafaeifard/2aea4493d15a42c19b6b34d1fa57db4d to your computer and use it in GitHub Desktop.
my first lambda use in c++
#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