Jump to content
  • Advertisement
Sign in to follow this  
DOS4dinner

A vector of different objects? (C++)

This topic is 3553 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

Advertisement
Yes it is, generally you want all objects of types B, C, D [...] to derive from type A and an object of type A is put into the vector. You can declare any and all polymorphic methods in A, pure or not. Otherwise you'll be doing upcasting which might not be feasible, but what you need will dictate your overall design so I can't offer advice now unless we all knew what you were trying to achieve.

Share this post


Link to post
Share on other sites
Yeah, but only through base and derived classes as stated above, you can't have ints and doubles mixed, and there is a reason that programming languages don't provide containers that allow you to clump random data types together, a) because it would mess with the memory allocation scheme, and b) because you wouldn't be able to do things like loop through a container and perform an operation on each member, because there would be no guarantee that the operation would be valid on any one member

Share this post


Link to post
Share on other sites
Quote:
Original post by Imtiaz
Yes it is, generally you want all objects of types B, C, D [...] to derive from type A and an object of type A is put into the vector.

Correction: you store pointers-to-A in the vector. Otherwise you'll end up slicing the derived object that you're storing in the vector, because only their base-class part can be stored.

Quote:
Original post by godsenddeath
and there is a reason that programming languages don't provide containers that allow you to clump random data types together, a) because it would mess with the memory allocation scheme, and b) because you wouldn't be able to do things like loop through a container and perform an operation on each member, because there would be no guarantee that the operation would be valid on any one member

Restriction #b mostly counts for statically typed languages, and some languages work around #a by storing references instead of objects.

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!