Pergunta

if ((char*)fixtureAData == "PLATFORM" && (char*)fixtureBData == "WEAPON"){
    static_cast<Weapon*>(contact->GetFixtureA()->GetBody()->GetUserData())->SetLethality(false);

This is a snippet of code from a team project I'm currently working on.

I was wondering about the functionality of static_cast on derived classes.

SetLethality is a method of Weapon, if I replace it with a virtual void called PlatformCollide, and create a version of that void in my dagger class, will the derived function be called? or does static_cast not work like that?

Foi útil?

Solução

First: (char*)fixtureAData == "PLATFORM" compares 2 pointers, it does not compare 2 strings. You need to use strcmp or strncmp. This may work if your compiler uses string pooling and fixtureAData is also assigned to the string literal "PLATFORM", but this isn't a good/safe assumption.

Second: If you are casting a derived class to one of its base classes (as I'm guessing you're doing, but it's not clear) and then calling a virtual function on that base class, it will indeed use the vtable to find the most derived function to call. Please clarify you're question if I'm making a false assumption about what you're doing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top