In other Objective-C classes I’m calling the code in a c++ class below. Sometimes the fSound object is something other than FMOD::Sound and it’s crashing when being released. How can I confirm that the fSound object is the proper type before I release it?
-(void) unloadSound:(FMOD::Sound *)fSound {
FMOD_RESULT result = FMOD_OK;
FMOD::Sound* soundEffect = static_cast<FMOD::Sound*>(fSound);
if (soundEffect) {
soundEffect->release();
}
soundEffect = NULL;
fSound = NULL;
}
You can dynamic_cast instead of static_cast (dynamic_cast returns NULL if the cast is unsuccessful).
However, you should review your design. Checking a type at runtime reveals a design problem that should be resolved with -for example- inheritance.