Skip to content

Instantly share code, notes, and snippets.

@snake66
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snake66/5888148f894a67e9be08 to your computer and use it in GitHub Desktop.
Save snake66/5888148f894a67e9be08 to your computer and use it in GitHub Desktop.
// 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