That makes a lot of sense! Thanks for the help! =)

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Ken Birdwell
Sent: Tuesday, August 19, 2003 12:47 AM
To: '[EMAIL PROTECTED]'
Subject: RE: [hlcoders] Probably basic question

The "MISC CODE" that looks like this:

        if ( !pOther->IsPlayer() )
        {
                return;
        }

is very important.  Casting towards the root of the hierarchy is generally a
safe thing to do in C++, whereas casting a base class pointer to a leaf
class pointer is usually a very bad thing to do, you really don't know what
the actual class the pointer points to.  The pOther pointer could just as
easily point to a monster or a button or whatever rather than a player, and
none of the CBasePlayer member functions or data would be valid.

The "correct" way to do this is to use RTTI - run time type information -
which is a C++ thing where all the pointers have builtin functions to
convert to their actual type and will return NULL if you try to do an
invalid cast.  Since HL doesn't use RTTI, it sometimes does a somewhat hacky
thing and the base class has a virtual IsPlayer() function that returns
false on all leaf classes, except for CBasePlayer where it returns true.
The slightly cleaner way to do it is the MyMonsterPointer() method, another
CBaseEntity virtual function that returns NULL for all classes, except for
ones that derive from CBaseMonster, which is sort of a crude version of
RTTI.

-----Original Message-----
From: Nick McLaren [mailto:[EMAIL PROTECTED]
Sent: Monday, August 18, 2003 7:08 PM
To: [EMAIL PROTECTED]
Subject: [hlcoders] Probably basic question



I asked this of a friend of mine, but I got the msg back due to his
mailserver being down. Hoping perhaps the list could help me out. I'm
aware that it is a bit of a newb question, but if you could help me out
just the same, I guarantee I will never have the same question twice ;)

It appears that the CBaseEntity pointer "pOther" gets converted to a
CBasePlayer pointer in the code below. Is this only possible whereas the
converted_to pointer-type is derived from the converted_from pointer-type?
Also, are the parentheses on the right-hand side of the assignment
necessary? Silly question I'm sure, but hey, I'm sure I get this 100% as I
go. ;)

void CBasePlayerItem::DefaultTouch( CBaseEntity *pOther )
{
/* MISC CODE HERE */

CBasePlayer *pPlayer = (CBasePlayer *)pOther;

/* MISC CODE HERE */

---------------------------
Nick McLaren, CCNA, SCSA
BattleLAN Technical Admin
<[EMAIL PROTECTED]>
http://www.battlelan.com/
---------------------------

_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders
_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives,
please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders


_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders

Reply via email to