Michele
Michele

Reputation: 681

How to dynamic_cast in objective c

I'd like to write this code on objective c:

    bool BordVertical::collisionwith( Jeu& jeu, ElementJeu& element )
{
    // Verify if the element is balle ype
    Balle* balle = dynamic_cast<Balle*>( &element ) ;
    if( balle )
    {
        balle->Vx( -balle->Vx() ) ;
        return true ;
    }
    return false ;
}

ball is a subclass of ElementJeu... Does anything similar exist in obj-c?

Thanks

Upvotes: 0

Views: 1354

Answers (1)

Koraktor
Koraktor

Reputation: 42923

You don't need it. Objective-C knows the type of your objects.

- (BOOL) collisionwith:(ElementJeu*)element {
    if ([element isKindOfClass:[Balle class]]) {
        [element setVx:[element getVx]];
        return YES;
    }
    return NO;
}

PS: jeu is redundant.

Upvotes: 3

Related Questions