开发者

Is there a meaning ascribed to where you put the pointer indicator "*" in Objective-C?

开发者 https://www.devze.com 2023-03-31 15:05 出处:网络
Here is an embarrassingly simple question that I have not been able to find an answer to as I delve into Objective C:

Here is an embarrassingly simple question that I have not been able to find an answer to as I delve into Objective C:

Is there a meaning ascribed to where you put the pointer indicator '*' when declaring or assiging a variable?

I have seen variables defined in different ways:

 NSString  *BLANK_SPACE = @" ";
 NSString const *BLANK_SPACE = @" ";
 NSString * const BLANK_SPACE = @" "; 

Now I know the meaning of the CONST modifier, but I put it there only because when I see the asterisk sepa开发者_Go百科rated by a space, it is usually before a CONST modifier.

Can someone explain the rationale about where to put the * when declaring/assigning a variable? What is the difference?


const is a postfix operator that refers to the thing to its left, as opposed to the right. If the const comes first then it applies to the first thing after it. const NSString *foo (as well as NSString const *foo) means that it's a non-const pointer to a const NSString - the pointer value can be reassigned, but the data being pointed to is immutable. NSString * const foo means that it's a const pointer to a non-const NSString - the data being pointed to can change but the location the pointer refers to cannot.

Spacing between the * and other parts of the line are just a matter of style and clarity.


The trick is to read the declarations from right to left. So:

NSString  *BLANK_SPACE 

Right to left... BLANK_SPACE is a pointer to an NSString.

NSString const *BLANK_SPACE

Right to left... BLANK_SPACE is a pointer to a const NSString.

NSString * const BLANK_SPACE

Right to left... BLANK_SPACE is a const pointer to an NSString.

Finally,

NSString const * const BLANK_SPACE

Right to left... BLANK_SPACE is a const pointer to a const NSString.


The position of the * relative to the variable name doesn't matter to the compiler. Some prefer to put the * with the variable, though, because it avoids possible confusion when declaring multiple variables:

char* a, b;  // a is char* and b is char, but both look like char*
char *a, b;  // looks a little more like the truth: a is char*, b is char

However you always refer to objects in Objective-C via pointers, so you'd never do something like the first line below:

NSString *a, b;  // WRONG: a is a pointer to an NSString object, b is just wrong.
NSString *a, *b; // OK: both a and b are pointers to NSString objects

The position of const, on the other hand, does make a difference:

int * const a; // a points to an int, and a can't be modified to point to some other int
int const * a; // a points to an int, and that int can't be changed

Now, const doesn't make a lot of sense with respect to objects. For one thing, NSString represents an immutable string, so declaring one const doesn't add much. For another, one generally modifies an object in Objective-C by sending a message, not by changing the object's ivars directly, and I don't believe the compiler will prevent changes made via messages. Therefore, a const pointer makes sense, a pointer to a const object, not so much:

NSString * const BLANK_SPACE = @" "; // makes sense; pointer BLANK_SPACE can't change
NSString const * BLANK_SPACE = @" "; // not useful
0

精彩评论

暂无评论...
验证码 换一张
取 消