Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of the following const definition

Usually I use the first one to define const, but I don't know the difference of the following clearly.

static NSString* kFetcherCallbackThreadKey = @"_callbackThread";

static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes";

NSString* const kFetcherRetryInvocationKey = @"_retryInvocation";

static const NSUInteger kMaxNumberOfNextLinksFollowed = 25;
like image 581
Emily Avatar asked Jul 18 '11 16:07

Emily


People also ask

What is the difference between const?

const and #define both are used for handle constants in source code, but they few differences. #define is used to define some values with a name (string), this defined string is known as Macro definition in C, C++ while const is a keyword or used to make the value of an identifier (that is constant) constant.

What is the meaning of const?

1. fixed and invariable; unchanging. 2. continual or continuous; incessant. constant interruptions.

What is the difference between const int and int const?

So in your question, "int const *" means that the int is constant, while "int * const" would mean that the pointer is constant. If someone decides to put it at the very front (eg: "const int *"), as a special exception in that case it applies to the thing after it.

What is the difference between const and macro?

The const keyword causes the identifier size to be allocated in the read-only memory. This means that the value of the identifier can not be changed by the executing program. MACROS are efficient than the const statements as they are not given any memory, being more Readable and Faster in execution!


1 Answers

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.

like image 152
Macmade Avatar answered Oct 16 '22 07:10

Macmade