简体   繁体   English

C ++转换为Objective-C

[英]C++ translated to objective-C

I'm looking into translating some code from C++ to Objective C and I ran into an instance that contains a function with a const notation at the end. 我正在研究将某些代码从C ++转换为Objective C,然后遇到一个实例,该实例的末尾包含一个带有符号的函数。 I'm quite rusty on C++ and I don't remember what this would represent (I have been googling though). 我对C ++相当生疏,不记得它代表什么(尽管我一直在谷歌搜索)。 I'd like to know how to force this over to Objective-C. 我想知道如何将其强加给Objective-C。 Currently, here's what I have: 目前,这是我所拥有的:

C++ code: C ++代码:

  float RenderingEngine1::RotationDirection() const
    {
        float delta = m_desiredAngle - m_currentAngle;
        if (delta == 0)
            return 0;

        bool counterclockwise = ((delta > 0 && delta <= 180) || (delta < -180));
        return counterclockwise ? +1 : -1;
    }

Objective-C: Objective-C的:

-(float)getRotationDirection{
    float delta = desiredAngle - currentAngle;
    if (delta == 0) {
        return 0;
    }

    bool counterclockwise = ((delta > 0 && delta <= 180) || (delta < -180));

    float test = counterclockwise ? +1 : -1;
    NSLog(@"%f",test );
    return counterclockwise ? +1 : -1; //problem

}

Edit: found the error of my ways and it was just an addition problem somewhere else in the program ('I love the easy ones'). 编辑:发现我的方式的错误,这只是程序中其他地方的一个附加问题(“我喜欢简单的方法”)。 That being said, I do want to ensure that the const declaration will not interfere with any further issues and want to check to ensure whether or not there should be any declarations I need to make (such as singleton methods and such). 话虽这么说,我确实要确保const声明不会干扰任何其他问题,并想检查以确保是否应该进行任何声明(例如单例方法等)。 Thank you guys for the answers! 谢谢你们的回答!

const in c++ means that this method doesn't alter the state of the object, thus it may use just const methods, and it can't alter class variables unless they're declared mutable. c ++中的const表示此方法不会更改对象的状态,因此它只能使用const方法,并且除非声明为可变的,否则无法更改类变量。

If your class is immutable, you can safely assume that every method you've declared is the equivalent of a const method. 如果您的类是不可变的,则可以放心地假设声明的每个方法都等同于const方法。

However, since you aren't altering any ivar, you're "translating" the code properly (logically speaking), as you aren't mutating any ivar. 但是,由于您没有更改任何ivar,因此您正在正确地“翻译”代码(从逻辑上讲),因为您没有在更改任何ivar。

I don't see any particular problem with your code, there shouldn't be a syntax (neither semantic) error. 我看不到您的代码有任何特定的问题,不应有语法(无语义)错误。

PS: That's not how you should compare floating point numbers, look here . PS:那不是比较浮点数的方法, 请看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM