what is the difference of the following const defintion
通常我使用第一个来定义const,但我不清楚下面的区别。
static NSString* kFetcherCallbackThreadKey = @"_callbackThread";
static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes";
NSString* const kFetcherRetryInvocationKey = @"_retryInvocation";
static const NSUInteger kMaxNumberOfNextLinksFollowed = 25;
In C, the static
keyword, used outside a function, is used to declare a symbol that will be accessible only from the file in which it's declared. Kind of «private» global variables.
The const keyword means «constant». Read, the value can't be modified. Note the two statements are different:
const int * x;
int * const x;
The first one defines a pointer to a constant integer (its value can't be modified, but it can point to something else). The second one defines a constant pointer to an integer (the pointer value can't be modified, but the value of the int may be). So you can perfectly have:
const int * const x;
So in your case:
static NSString* kFetcherCallbackThreadKey = @"_callbackThread";
A pointer to a NSString instance that will be accessible only from the file in which it's declared.
static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes";
A constant pointer to a NSString instance that will be accessible only from the file in which it's declared.
NSString* const kFetcherRetryInvocationKey = @"_retryInvocation";
A constant pointer to a NSString instance that may be accessed from other files of your project.
static const NSUInteger kMaxNumberOfNextLinksFollowed = 25;
A constant integer that will be accessible only from the file in which it's declared.
static
means that the variable is accessible only within the compilation unit it's declared in - essentially this source file. const
means its value can never change. You can use one, both, or none depending on what you're looking for.
This is a static string which will be the same reference for all instances of the class (static). If you change it in one instance, it will change in all other instances.
static NSString* kFetcherCallbackThreadKey = @"_callbackThread";
This is an NSString pointer to a constant object that is also shared between all instances (static). The const
directive makes the variable immutable.
static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes";
This is a pointer to a constant NSString object. It could have a different instance for each class (if NSStrings are not interned by the compiler, I'm not sure if they are), but cannot be changed (const).
NSString* const kFetcherRetryInvocationKey = @"_retryInvocation";
This is a constant static integer. It will be shared between all instances of the class (static) and cannot be changed (const).
static const NSUInteger kMaxNumberOfNextLinksFollowed = 25;
链接地址: http://www.djcxy.com/p/53726.html
上一篇: 我如何使用bean的属性格式化字符串
下一篇: 下面的const定义有什么区别