甚至重要吗? Const before or const after?我猜我是否在const
之前或之后放CGFloat
它会使CGFloat
的值保持不变,但指针呢?这是否适合Objective-C:
// Example.h
extern CGFloat const kPasscodeInputBoxWidth;
// Example.m
CGFloat const kPasscodeInputBoxWidth = 61.0f;
答案 0 :(得分:21)
可以在之前或之后进行。在指针的情况下,重要的是const
是否在星号之前或之后结束:
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.
答案 1 :(得分:5)
没关系(我一直使用前者,但我想这是风格问题):
const CGFloat kPasscodeInputBoxWidth = 61.0;
CGFloat const kPasscodeInputBoxWidth = 61.0;
至少在CGFloat
的当前版本中,它只是double
的typedef,所以就像使用常规原始数据类型一样。对于指针,const的放置将决定它是指针还是恒定的值,因此它确实很重要。