Is it possible to use std::for_each or anything else like that ?
#include <list>
#include <algorithm>
// Class declaration
//
struct Interface
{
virtual void run() = 0;
};
struct A : public Interface
{
void run() { std::cout << "I run class A" << std::endl; }
};
struct B : public Interface
{
void run() { std::cout << "I run class B" << std::endl; }
};
// Main
//
int main()
{
// Create A and B
A a;
B b;
// Insert it inside a list
std::list<Interface *> list;
list.push_back(&a);
list.push_back(&b);
// Then execute a func with for_each without recreate a func which call Interface::run()
std::for_each(list.begin(), list.end(), run);
return 0;
}
Edit :
My question is : How can I call each run() member function inside a loop using algorithm or a more simply a C++ way without using iterators…
You can do this using
std::mem_fun: