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)

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #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
 


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 07:40 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 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3