Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Development

Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum)

Reply
 
Thread Tools Display Modes
  #1  
Old 10-31-2012, 09:54 AM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default 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.
Reply With Quote
  #2  
Old 10-31-2012, 11:32 AM
joligario's Avatar
joligario
Developer
 
Join Date: Mar 2003
Posts: 1,497
Default

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.
Reply With Quote
  #3  
Old 10-31-2012, 12:10 PM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default

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.

Last edited by c0ncrete; 10-31-2012 at 12:10 PM.. Reason: corrected syntax
Reply With Quote
  #4  
Old 10-31-2012, 01:19 PM
lerxst2112
Demi-God
 
Join Date: Aug 2010
Posts: 1,742
Default

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.
Reply With Quote
  #5  
Old 10-31-2012, 02:51 PM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default

thanks. i -think- you confirmed my understanding. haha.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 09:01 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3