It works that way.
#include <algorithm>
#include <iostream>
#include <functional>
#include <vector>
struct A {
virtual void foo() {
std::cout << "A::foo()" << std::endl;
}
};
struct B: public A {
virtual void foo() {
std::cout << "B::foo()" << std::endl;
}
};
int main()
{
std::vector<A*> VectorOfAPointers;
VectorOfAPointers.push_back(new B());
std::for_each(VectorOfAPointers.begin(), VectorOfAPointers.end(), std::mem_fun(&A::foo));
return 0;
}
prints
B::foo()
Thus, he does exactly what you want. Make sure keywords are virtual
present, but easy to forget.
source
share