Does const go before or after CGFloat?
-
28-10-2019 - |
Question
Does it even matter? Const before or const after? I'm guessing that whether I put const
before or after CGFloat
it makes the value of CGFloat
constant, but what about the pointer? Is this right for Objective-C:
// Example.h
extern CGFloat const kPasscodeInputBoxWidth;
// Example.m
CGFloat const kPasscodeInputBoxWidth = 61.0f;
Solution
It can go either before or after. In the case of a pointer, what matters is whether the const
ends up before or after the asterisk:
const int *a; // pointer to const int -- can't change what a points at
int const *a; // same
int *const a; // const pointer to int -- can't change the pointer itself.
// Note: must be initialized, since it can't be assigned.
OTHER TIPS
It doesn't matter (I've always used the former, but I guess it's a matter of style):
const CGFloat kPasscodeInputBoxWidth = 61.0;
CGFloat const kPasscodeInputBoxWidth = 61.0;
At least in the current rendition of CGFloat
, it's just a typedef of double
, so do as you would with a regular primitive datatype. For pointers, the placement of const will determine if it's the pointer or the value that is constant, so for that it does matter.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow