   Simple override problem [SOLVED]
  My game has an Entity vector it uses to organize all objects in the world which are not static tiles or images.Player, Enemy, Vehicle, Particle etc are all subclasses of type Entity. However, the vector is of type Entity. Now I've overridden the Entity animate() function in each subclass, but when I call entityVector.at(i).animate(), it only calls the top level Entity.animate() function and not Player.animate() or Enemy.animate() as I would like it to.Is this behavior because the vector is of type Entity? How can force my game to call the overridden functions?Thanks.EDIT: This is C++ by the way.[Edited by - Nate Hale on May 10, 2010 12:25:37 AM]
  as i see from the code you use objects in vector. use pointers and see if that works:std::vector<Entity *> entityVector;;  As Kasya has touched on, dynamic dispatch is accessible through pointers in C++:#include <iostream>struct Entity {void animate() {std::cout << "Entity::animate" << std::endl;}virtual void vanimate() {std::cout << "Entity::vanimate" << std::endl;}};struct Player : Entity {void animate() {std::cout << "Player::animate" << std::endl;}void vanimate() {std::cout << "Player::vanimate" << std::endl;}};int main() {Entity e;e.animate();e.vanimate();Entity p = Player();p.animate();p.vanimate();Entity * pe = new Entity();pe->animate();pe->vanimate();delete pe;Entity * pp = new Player();pp->animate();pp->vanimate();delete pp;}Output:Entity::animateEntity::vanimateEntity::animateEntity::vanimateEntity::animateEntity::vanimateEntity::animatePlayer::vanimateThat being said, you need to be careful to manage your memory when using a collection of pointers. I recommend looking at the Boost Pointer Containers library or a collection of TR1/Boost Smart Pointers.[edit] Also, I would take a look at standard library algorithms, particularly for_each (or transform, depending on what you're doing) to apply a function against a collection.[edit2] Speaking of memory management, forgot the deletes (although the runtime very likely reclaims the memory on exit).[Edited by - jflanglois on May 9, 2010 12:08:49 PM];  I tried using a vector of Entity pointers, but since this collection contains particles and enemies, which are deleted after a certain condition is met, I'd prefer it to be a collection of objects.That said, the Entity class's functions are virtual, so I'm not sure why it's overridden subclass functions are not being called.   ;Quote:Original post by Nate HaleI tried using a vector of Entity pointers, but since this collection contains particles and enemies, which are deleted after a certain condition is met, I'd prefer it to be a collection of objects.That said, the Entity class's functions are virtual, so I'm not sure why it's overridden subclass functions are not being called.If you're storing your entities by value, e.g.:std::vector<Entity> entities;Then the problem is probably slicing.Can you explain in more detail why you don't want to use pointers? Whatever your concern might be, there's probably a way that it can be addressed.   ;Quote:Original post by jykCan you explain in more detail why you don't want to use pointers? Whatever your concern might be, there's probably a way that it can be addressed.Originally, I did use a vector of Entity pointers, but consider a typical case in which the screen is populated by 2 players5 enemies10 projectilesafter a certain time, either the enemies or players will be destroyed and the projectiles will expire. The point is, at any given time, any number of Entities might be created or destroyed. If I were to use a collection of pointers to Entities, I would still have to have another collection of entities they pointed to. Essentially, I would need a vector of pointers pointing to individual elements of another vector of Entities. I would still have a vector of Entities.As far as I can tell, just using a single vector of Entities is simpler.EDIT: Having read up on that link about slicing (thank you for that), I think this is the cause of the problem, but my design problem remains. I would like the ability to delete/add Entity objects from memory during runtime. Suggestions?   ;Quote:If I were to use a collection of pointers to Entities, I would still have to have another collection of entities they pointed to. Essentially, I would need a vector of pointers pointing to individual elements of another vector of Entities. I would still have a vector of Entities.This doesn't make any sense. You don't need a collection of entity pointers that points to a collection of entities, you just need a collection of pointers.When you want to add a new entity to your collection:std::vector<Entity*> entityPointers;entityPointers.push_back(new EntityBasedClass();)Then just kick the pointer out of your vector when it is dead (but be sure to delete it before you drop the pointer!)If you don't agree with this maybe state why you think you have to have two collections as you've described?   ;Quote:If I were to use a collection of pointers to Entities, I would still have to have another collection of entities they pointed to. Essentially, I would need a vector of pointers pointing to individual elements of another vector of Entities. I would still have a vector of Entities.I think the piece of the puzzle that you're missing is dynamic creation of objects via new, as mentioned by karwosts.In short, you don't need multiple containers; you just need a single container of Entity*'s. The Entity*'s don't point to pre-existing objects stored elsewhere, but rather to objects created and constructed via new (again, as shown by karwosts).The added complication here is that what is new'ed must be delete'd. (Well, it doesn't *have* to be, but it should be.) You can do this manually, but it's usually advisable to use an RAII container for this (such as the Boost 'ptr' containers, or a container of smart pointers) to simplify the clean-up process.   ;  Hey, that worked! I guess I was overthinking memory management when I didn't need to. I learned a lot about pointers and inherent dangers in polymorphism too!Thanks so much, everyone!   