Skip to content

Instantly share code, notes, and snippets.

@mheffner
Created December 14, 2015 20:15
Show Gist options
  • Save mheffner/629d50107130cd956b69 to your computer and use it in GitHub Desktop.
Save mheffner/629d50107130cd956b69 to your computer and use it in GitHub Desktop.
c++ functional interface
#include <functional>
#include <iostream>
class Foo {
public:
Foo(std::function<double()> gauge) {
this->_gauge = gauge;
}
void invoke() {
double g = _gauge();
std::cout << "calling gauge returned: " << g << std::endl;
}
std::function<double()> _gauge;
};
void call_it(Foo *foo) {
foo->invoke();
}
int main() {
double val = 4.5;
class Foo *f = new Foo([&val] {
std::cout << "calling function " << std::endl;
return val;
});
call_it(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment