C++ polymorphism question
OK lets say I have a base class Class1 and two derived classes Class2 and Class3.
Now I can make a pointer to an object of Class2 on the heap like this:
Class1* p_aClass = new Class2();
my question is, if I had an array of pointers could some point to a Class2 on the heap and some point to Class3 on the heap since as far as the pointer knows it is pointing to Class1?
I know this question seems a little strange and I dont have a specific example but I am just trying to understand the different ways that being able to point to the base class and get to the derived class can be used.
Thanks
Yes, yes you may.
class a_class {};
class b_class : public a_class {};
class c_class : public a_class {};
vector<a_class*> aList;
aList.push_back(new b_class);
aList.push_back(new c_class);
// you can now iterate through aList
class a_class {};
class b_class : public a_class {};
class c_class : public a_class {};
vector<a_class*> aList;
aList.push_back(new b_class);
aList.push_back(new c_class);
// you can now iterate through aList
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement