overriding and casting
i'm still learning C++ and i'm having a little bit of trouble wrapping my head around something. i've seen areas where group members are being iterated over as mobs, IsClient() is checked, and if true, a Client method is called on the mob through CastToClient(). can the check and cast be skipped if the method being called is overridden in the Client class? is it only necessary when the method being called is unique to Client? i'm wondering because IsClient() is only set to return a value of true in the definition of Client, but is false in Entity and Mob, and this leads me to believe the client check and cast would be superfluous.
for example, if i wanted to override Mob::IsEngaged() in client.h with Code:
virtual inline bool IsEngaged() { return (IsAIControlled() ? !hate_list.IsEmpty() : AggroCount > 0); thanks in advance. |
I'm guessing you mean overload. I'm not near the source right now, but if I am understanding you right, you can always stick that return as the first conditional check within the function.
|
i'm not familiar with the proper terminology, honestly. i've seen both and i guess i just chose the one that made the most sense in english to me. :)
regardless, i'm not really sure i understand your response. i'm trying to determine if i can forgo checking the value of group->members[i]->IsClient() and then checking for the value of group->members[i]->CastToClient()->AggroCount in a group with both bots and clients by adding the previous inline to the Client class, since group->members[i] is a pointer to a Mob. would it return the value of the new Client member without the explicit cast, or would it still be using IsEngaged() as defined in Mob? thanks for the response. |
So, let's take two examples:
Using a base class pointer to call a virtual function: Code:
class Mob Conditionally calling a child only function: Code:
class Mob To answer your specific question, I would guess you probably want to make IsEngaged() virtual in Mob and override it in the Client and possible Bot cases. Then you would just call it using a Mob pointer without any IsClient or CastToClient involved. This would change the behavior of anyplace that calls that function though, so you'd need to be sure nobody was calling it on a client and expecting the existing result. |
thanks. i -think- you confirmed my understanding. haha.
|
All times are GMT -4. The time now is 06:37 PM. |
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.