1

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. I'm quite rusty on C++ and I don't remember what this would represent (I have been googling though). I'd like to know how to force this over to Objective-C. Currently, here's what I have:

C++ code:

  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:

-(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). Thank you guys for the answers!

TheCodingArt
  • 3,436
  • 4
  • 30
  • 53
  • 1
    See [here](http://stackoverflow.com/questions/1549869/what-does-const-mean-following-a-function-method-signature) – juanchopanza Mar 05 '13 at 20:19
  • http://stackoverflow.com/questions/3868106/does-an-approximation-of-cs-const-methods-exist-for-objective-c – Pradheep Mar 05 '13 at 20:20
  • 3
    Just a comment... Did you know that you can use Objective-C++ to make a connector? so you can compile your c++ code directly without need of translating it – Pedrom Mar 05 '13 at 20:21
  • 1
    What is the problem? Please post the error, your Objective-C version works on my side. And that const at the end ensures that you are not modifying any member of your class inside this method. – J_D Mar 05 '13 at 20:25
  • The issue is I'm returning different variables on the C and C++ end and want to ensure that the const declaration isn't causing the difference. It's being quite a minor headache. – TheCodingArt Mar 05 '13 at 20:33
  • @Pradheep, that would apply if I was writing out an accessor for something. In this case it's a method (not a class of sorts). That's where everything starts throwing off in translation. – TheCodingArt Mar 05 '13 at 20:54
  • @juanchopanza That was a good and very informative. Thank you for the link. I guess I do have some reading up to identify the purpose of this. Not really sure why I'd want to use it unless I wanted to prevent copying data back and forth. – TheCodingArt Mar 05 '13 at 21:00

1 Answers1

1

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.

If your class is immutable, you can safely assume that every method you've declared is the equivalent of a const method.

However, since you aren't altering any ivar, you're "translating" the code properly (logically speaking), as you aren't mutating any 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.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • In this particular reason (due to the end result and such) it honestly should not matter although this code is taken from an O'reilly book (so I'm not the unorthodox culprit lol). Thank you for the information though. I just really need to ensure that this won't cause for issues and my code is fine. – TheCodingArt Mar 05 '13 at 20:46