1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <iostream> #include <vector> using namespace std; struct Base { virtual int index() { return -1; }; }; struct D1 : public Base { int idx = 1; virtual int index() { return idx; } }; int main(void) { vector<Base*> v; D1* d = new D1; v.push_back(d); cout << v.back()->index() << endl; return 0; }