C++ polymorphism question

Started by
2 comments, last by Hakiko 18 years, 11 months ago
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
Advertisement
yes

and no problem.
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.
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
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]
Thanks
I think I can use this to activate verbs from my parser. I can tokenize the verbs and use the number as an index to call them from the array.

This topic is closed to new replies.

Advertisement