Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vector of polymorphic objects...

Started by Ruzhyo2000 Mar 29, 2011 at 7:45 PM 5 replies 1.5k views
Original Post
Ruzhyo2000
Ruzhyo2000
As I have discovered recently, having a vector of polymorphic objects doesn't work. If classes B, C, and D inherit from A they will all behave like an A object in a vector of A's because the part of them that was B, C, or D is chopped off... is there some substitution for this?

I have a class that has as a member a vector of virtual objects. This virtual object has between 20-40 objects that inherent from it. Is there any clean way to do this so that these classes will behave like they are supposed to, instead of like the one virtual class they inherit from? I know now a vector won't work, and I was told I could use a vector of pointers, and have one vector for each type of derived object that those pointers point to, but that would be very messy. Is there any other way?
karwosts
karwosts
You don't need to have one vector for each type of derived object if you use pointers.

If you just have a single vector of than when you call functions on these pointers it will use the correct polymorphic function. You can throw A's and B's and C's and D's all together into the vector of A*, and everything should work as you expect.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Ruzhyo2000
Ruzhyo2000

You don't need to have one vector for each type of derived object if you use pointers.

If you just have a single vector of than when you call functions on these pointers it will use the correct polymorphic function. You can throw A's and B's and C's and D's all together into the vector of A*, and everything should work as you expect.


So... am I using new at those pointers? Like do I do:

myAptrVect = new B;

or something like that? If so I just free the memory using delete on every element in the vector, right?
karwosts
karwosts
Yes, that's correct. new B returns a B* pointer, which will happily insert itself into an A* container.

Just remember to call delete on the pointers when you're done with them or you'll have a memory leak.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Ruzhyo2000
Ruzhyo2000
I feel like a major dummy for not thinking of that before! Duh! This makes things so much simpler now. Thanks!
Ravyne
Ravyne
Yep. Whenever you remove an object from the vector, you have to remember to deallocate its memory.

If there's a readily-identifiable trait of the object that indicates whether an object is to be removed, then you can partition the vector using remove_if, and then deallocate all "removed" objects in one go (remove_if doesn't deallocate anything, just prepares items for removal by lumping them together at the end of the vector.) Optionally, with a little extra programming work, its often possible to keep those "removed" items around and re-use them later to avoid extraneous allocations/deallocations.
throw table_exception("(? ???)? ? ???");
rip-off
rip-off
You might consider a container of smart pointers (e.g. std::shared_ptr<> or boost::shared_ptr<>) or a smart container (e.g. boost::ptr_vector<>).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.