views:

46

answers:

1

Two things are strange with NSDecimalAdd(). First, when I search for examples, people seem to provide parameters by reference like NSDecimalAdd(&foobar, &foo, &bar, ....) and so on. The second strange thing is this const. Why's the parameter saying it wants a constant there? And why does this not apply for result?

NSCalculationError NSDecimalAdd (
   NSDecimal *result,
   const NSDecimal *leftOperand,
   const NSDecimal *rightOperand,
   NSRoundingMode roundingMode
);

Maybe someone can make the secret "public" here...

+2  A: 

const means the API will not change the value of the parameter you are passing in there. result, however, is where the result of the routine will go, so it must change -- hence no const.

fbrereto
HelloMoon
I also believe that the structs are passed in by reference as an optimization, so that you don't have the overhead of copying them into the function.
Brad Larson