Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "static NSInteger const" and "static const NSInteger"?

Tags:

objective-c

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?

like image 762
Wei Avatar asked May 12 '14 08:05

Wei


1 Answers

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
like image 87
marinus Avatar answered Oct 21 '22 09:10

marinus