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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -