This is a question about how to implement some needs I have been having lately. I am sure there is a pattern or common solution for this and, even though I've come with one, I am eager to learn more.
Suppose I am working in a game in which all entities related to the game itself are derived from a class "Actor" (say "obstacle", "moving obstacle", "projectile" and "cannon"). In game, all those entities are stored in a std::vector<Actor *>
vector so they can be traversed.
Now, let's suppose each "Actor" can "do" something at each turn and let's give them a method "act". Obstacle::act would do little, Moving_obstacle::act and Projectile::act would move them around and "Cannon::act" would create a new projectile. It sort of 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 have them all "acting". Well, so far so good... The thing is, Cannon::act could have a different prototype or return value (for example, to store a generated projectile and leter have it pushed into the vector) and this one "small" difference breaks it all.
Now, I know that from certain standpoint these method overloads are completely different functions each. I also know that one can always plan in advance and engineer through the problem with enough foresight... Or one can just manuever around the problem.
In this case at hand, I just used different unique identificators for each derived Actor class and used them to cast to the correspondent class and do the work around them. I am sure I will come around the same problem again and I am curious to know about some entry level solutions.
Thanks in advance for your time.
The derived class inherits all variables and methods, which are public. It does not inherit those variables and methods, which are private. However, we do not want to declare all variables and methods to be public, since they will be visible for everybody. Therefore, Java offers another modifier called protected .
Property and method overridingA property or method defined in a base class is accessible in the derived class. You can also modify the behavior of the base class properties and methods used by the derived class. This is called property overriding and method overriding.
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.
Explanation: A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer because object slicing happens when a derived class object is assigned to a base class object.
The start is sound, you derail here:
The thing is, Cannon::act could have a different prototype or return value (for example, to store a generated projectile and leter have it pushed into the vector)
Why would it? Act is act. The instance must figure it out without anything else. You shall train it in the ctor, or other calls before acting. Or it shall look around in during the Act call.
Consider: even if you magically had the payload for different params ready, in the quoted while how would it be figured out? The call is abstract. Even if you infested it by some dynamic_cast, still leave the problem which cannon shall get which params?
No, the subjects must cooperate with each other, or use some messaging system (see dispatcher)...
the internal variables that the body of each act is different, and they can use internal private owned/ or inherited variable, so if you can set these variables before calling act, so you don't need to send anything through the parameter to be safe, or to not get in complications
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With