[ create a new paste ] login | about

Link: http://codepad.org/lb2u01Ap    [ raw code | output | fork ]

fenrir - C++, pasted on Apr 3:
#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;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
A::f1
2
A::f2
4
A::f1
4
A::f2
8
B::f1
24
A::f2
12


Create a new paste based on this one


Comments: