Jump to content
  • Advertisement
Sign in to follow this  
Hakiko

C++ polymorphism question

This topic is 4900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!