I want to create a std::function object for the parent class's version of a virtual and overridden function, see the following example:
#include <iostream>
#include <functional>
class Parent
{
public:
virtual void func1()
{
std::cout << "Parent::func1\n";
}
virtual void func2()
{
std::cout << "Parent::func2\n";
}
};
class Child : public Parent
{
public:
// overrides Parent::func1
virtual void func1()
{
std::cout << "Child::func1, ";
Parent::func1();
}
// overrides Parent::func2
virtual void func2()
{
std::cout << "Child::func2, ";
std::function< void() > parent_func2 = std::bind( &Parent::func2, this );
parent_func2();
}
};
int main()
{
Child child;
child.func1(); // output: Child::func1, Parent::func1
child.func2(); // output: Child::func2, Child::func2, ...
return 0;
}
While the call to child.func1()
behaves as expected, the call to child.func2()
becomes an infinite recursion, where parent_func2()
seems to call Child::func2()
instead of Parent::func2()
to which I intended to bind it.
Any idea how I can have parent_func2()
really call Parent::func2
?