#include <iostream>
#include <vector>
#include <utility>
using namespace std;
struct A {
int v;
virtual int f1(const int &_v){
cout << "A::f1" << endl;
return v * _v;
}
virtual int f2(const int &_v){
cout << "A::f2" << endl;
return v * _v * _v;
}
};
struct B : public A {
virtual int f1(const int &_v){
cout << "B::f1" << endl;
return v * _v * _v * _v;
}
};
int main(){
A a[2];
B b;
A *a_ptr[] = {&a[0], &a[1], &b};
for(unsigned int i(0); i < sizeof(a_ptr) / sizeof(a_ptr[0]); i++){
a_ptr[i]->v = i + 1;
}
typedef vector<pair<A *, int (A::*)(const int &)> > vec_t;
vec_t vec;
for(unsigned int i(0); i < sizeof(a_ptr) / sizeof(a_ptr[0]); i++){
for(int j(0); j < 2; j++){
vec.push_back(vec_t::value_type(a_ptr[i], j % 2 == 0 ? &A::f1 : &A::f2));
}
}
for(vec_t::const_iterator it(vec.begin()); it != vec.end(); ++it){
cout << ((it->first)->*(it->second))(2) << endl;
}
return 0;
}