Vector of objects from two subclasses of a common class

Hello all,

I'd like to have a vector in which at runtime objects which are instances of two subclasses of a common class should be inserted, as follows:

1
2
3
4
5
6
7
8
9
10
11
class A {...}
class A1 : A {...}
class A2 : A {...}

vector<A> myVector;
A1 a1 = new A1();
A2 a2 = new A2();
A a = new A();
myVector.push_back(a1);
myVector.push_back(a2);
myVector.push_back(a);


Is this possible in C++? If yes, how to do it? The way I sketched it above doesn't seem to work, it always complains about a1/a2 not being of type A.

Thank you!
Solved!! My bad, should have been vector<A*> because I deined the objects as A1 *a1 = new A1();.
Thanks anyway!

PS. For other interested people: http://www.cplusplus.com/forum/general/1715/
Last edited on
Topic archived. No new replies allowed.