16

这还重要吗?const 之前还是 const 之后?我猜我是否在它const之前或之后放置CGFloat它会使值成为CGFloat常量,但是指针呢?这是否适用于 Objective-C:

// Example.h

extern CGFloat const kPasscodeInputBoxWidth;


// Example.m

CGFloat const kPasscodeInputBoxWidth = 61.0f;
4

2 回答 2

23

它可以在之前或之后进行。在指针的情况下,重要的是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.
于 2011-05-03T20:47:31.453 回答
6

没关系(我一直使用前者,但我想这是风格问题):

const CGFloat kPasscodeInputBoxWidth = 61.0;
CGFloat const kPasscodeInputBoxWidth = 61.0;

至少在 的当前版本中CGFloat,它只是 的 typedef double,因此可以像使用常规原始数据类型一样进行操作。对于指针, const 的位置将决定它是指针还是常量值,所以这很重要。

于 2011-05-03T20:44:06.097 回答