This is a question about how to realize some of the needs that I had recently. I am sure that there is a sample or a general solution for this, and although I came with it, I really want to know more.
Suppose I work in a game in which all entities related to the game itself come from the class "Actor" (for example, "obstacle", "moving obstacle", "projectile" and "gun"). In the game, all these objects are stored in the std::vector<Actor *> so that they can pass.
Now suppose that each βActorβ can βdoβ something at every step and give them a method of βactingβ. Obstacle :: act will do little, Moving_obstacle :: act and Projectile :: act will move them, and "Cannon :: act" will create a new shell. It makes sense to have a pure virtual function Actor :: act, so I can, in turn, do something like:
std::vector<Actor *>::iterator b=myvectorofactors.begin(), e=myvectorofactors.end(); while(b < e) { *b->act(); b++; }
And let them all "act." Well, so far so good ... The fact is that Cannon :: act can have a different prototype or return value (for example, to store the generated projectile and leter it is pushed into the vector), and this "small" difference breaks it all.
Now I know that from a certain point of view, these method overloads are completely different functions. I also know that you can always plan and work out a problem in advance with sufficient foresight ... Or you can simply deal with the problem.
In this case, I simply used different unique identifiers for each derived class of the Actor and used them to translate to the corresponding class and executed them. I am sure that I will start the same problem again, and I am curious to learn about some entry-level solutions.
Thanks in advance for your time.