Vector Filled with Classes

Let's say I have a sprite class. Sprite has draw() in it. There are many sub-classes of sprite (for example, monster1 and monster2).

Sprite's draw() has a basic generic rendering code.
monster1 does not define its own draw(), it uses the one it inherits from Sprite
monster2 has a special (redefined) draw().

I have
vector<Sprite*> sprites
that is filled with tons of monster1s, monster2s, and so on.

My question - when I go through the vector and call every entry's draw() like so:
1
2
3
for(int i=0;i<sprites.size();i++){
    sprites[i]->draw();
}


Sprite's version of draw is always called, even though monster2 (and many others) have their own version of draw().

How should I go about calling each sub-class's respective draw()? Thanks!
¿have you declared 'draw()' as virtual?

> There are many sub-classes of sprite (for example, monster1 and monster2).
that may be a misuse.
Consider using composition instead (a monster has an sprite)
Researching both right now - bear in mind that I'm a hobbyist learning by himself so I'm fairly lacking in C++ skill. Thanks for the direction though!

Update:
virtual was exactly what I wanted / needed. It was literally 1 word that made everything work!

I had heard of it long ago but since forgotten. Thanks a ton!
Last edited on
Topic archived. No new replies allowed.