Usually, I have seen the code that to declare constants was just like this:
static const NSInteger kNum = 1;
static NSString * const kStr = @"A";
I know why const
should be written behind NSString *
, but I'm not sure if static NSInteger const kNum = 1;
and static const NSInteger kNum = 1;
are same.
Is there any difference between static NSInteger const
and static const NSInteger
?
They are the same. A constant type can be "derived" from a given type by writing one of:
const type
type const
The order does not matter in this case.
These are a variable pointers to constant data:
type const* var
const type* var
Which means that the pointer can be changed, but the data can't (unless you cast it). You can read it right-to-left as "pointer to constant type".
This is a constant pointer to variable data:
type* const var
Note the binding.
Which means that the pointer cannot be made to point elsewhere, but you can change the data. You can read that right to left as "constant pointer to type".
It will come as no surprise that making both the pointer and the data constant can be done like this:
type const* const var
const type* const var
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With