#include <iostream>
#include <functional>
#include <iomanip>
usingnamespace std;
typedef function<void()> func;
func f;
class Parent
{
public:
Parent()
{
f = std::bind(&Parent::callMe,this);
f();
}
virtualvoid callMe()
{
cout << "parent" << endl;
}
};
class Child : public Parent
{
virtualvoid callMe()
{
cout << "child" << endl;
}
};
int main ()
{
Child c;
f();
}
parent
child
The output above is how it behaves in VS2012. This is the behavior I would expect. I desire/expect the below behavior:
- f can legally bound to 'callMe', even in the Parent's ctor
- The function is treated as virtual even when called through the function pointer. So the child class's override is called once Child has been constructed.
My question is... is this standard behavior? Is this how this will work everywhere? Or am I doing something undefined?