EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Development::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=590)
-   -   overriding and casting (https://www.eqemulator.org/forums/showthread.php?t=35937)

c0ncrete 10-31-2012 09:54 AM

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);
would i be able to skip IsClient() and CastToClient() for each group member?

thanks in advance.

joligario 10-31-2012 11:32 AM

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.

c0ncrete 10-31-2012 12:10 PM

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.

lerxst2112 10-31-2012 01:19 PM

So, let's take two examples:

Using a base class pointer to call a virtual function:
Code:

class Mob
{
    virtual bool Something() { return false; }
}

class Client : public Mob
{
    virtual bool Something() { return true; }
}

bool DoSomething( Mob* TheMob )
{
    return TheMob->Something();
}

In this case if TheMob is a pointer to a Mob it will call the function that returns false, but if TheMob is a pointer to a client it will call the function that returns true. This is generally the best way to handle things if the function can be implemented in the base class and overridden in children classes where a different behavior is desired.

Conditionally calling a child only function:
Code:

class Mob
{
}

class Client : public Mob
{
    virtual void ClientOnlyFunction() { /*Do something exciting here*/ }
}

void DoSomething( Mob* TheMob )
{
    if( TheMob->IsClient() )
    {
        TheMob->CastToClient()->ClientOnlyFunction();
    }
}

If for whatever reason you can't implement a function in the base class then this is the way you'd need to make sure you only call it on clients. Generally this isn't the best way to handle things, since accidentally calling CastToClient() on something that isn't a client will probably crash, but even if it doesn't it is undefined behavior. A safer way to handle it would be to use dynamic_cast to cast to the child, check if it succeeded, and if it did then use that pointer to call the child only function. Of course, such safety comes with a price as dynamic_cast has a cost where static_cast doesn't.

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.

c0ncrete 10-31-2012 02:51 PM

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.