Last active
August 29, 2015 14:27
-
-
Save snake66/5888148f894a67e9be08 to your computer and use it in GitHub Desktop.
This file contains 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
// Simple test for using std::function with inheritance | |
#include <functional> | |
#include <iostream> | |
class Base | |
{ | |
public: | |
virtual void do_it(const std::string & s) | |
{ | |
std::cout << "Doing " << s << std::endl; | |
} | |
}; | |
class Derived : public Base | |
{ | |
public: | |
void do_it(const std::string & s) override | |
{ | |
for (int i = 0; i < 5; ++i) | |
{ | |
std::cout << i << ": "; | |
Base::do_it(s); | |
} | |
} | |
}; | |
typedef std::function<void(const std::string &)> mfunc; | |
int main() | |
{ | |
using namespace std::placeholders; | |
Base b; | |
Derived d; | |
mfunc f1 = std::bind(&Base::do_it, b, _1); | |
mfunc f2 = std::bind(&Base::do_it, d, _1); | |
f1("important work..."); | |
f2("important work 5 times"); | |
auto f3 = std::bind(&Base::do_it, b, _1); | |
f3("a bit of time off..."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment