开发者

How to dynamic_cast in objective c

开发者 https://www.devze.com 2023-02-09 01:01 出处:网络
I\'d like to write this code on objective c: bool BordVertical::collisionwith( Jeu& jeu, ElementJeu& element )

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?

Than开发者_StackOverflowks


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消