c++ - Check if A is a subclass of B? -
i using unreal engine 4 , seems can't avoid casts.
acontroller* c = this->getcontroller(); aplayercontroller* p = (aplayercontroller*)c;
is there way can check if c
playercontroller
before cast?
like lot of game engines, unreal engine compiled without rtti performance reasons, dynamic_cast
not work.
unreal engine provides own alternative, called cast
. can't find documentation right now, this question describes use nicely.
acontroller* c = this->getcontroller(); aplayercontroller* p = cast<aplayercontroller>(c); if (p) { ... }
acontroller
has convenience method casttoplayercontroller
same thing:
acontroller* c = this->getcontroller(); aplayercontroller* p = c->casttoplayercontroller(); if (p) { ... }
if sure c
going aplayercontroller
castchecked
more efficient:
acontroller* c = this->getcontroller(); aplayercontroller* p = castchecked<aplayercontroller>(c); ...
in debug builds, use cast
, throw assert if return null; in release builds, resolves fast static_cast
.
Comments
Post a Comment