开发者

In objective-C what's the difference between Type* var, and Type *var?

Oh the joys of being a memory management noob !

I'm getting bit by some Objective-C code, eventho开发者_如何学运维ugh I understand the basics of pointers, I've seen these two different constructs, without being able to really understand their difference.

Can anyone enlighten me ?

Edited my question, the constructs didn't behave differently, instead I got bit yet again by the multiple declarations gotcha. Thanks !


There’s no difference – it’s a matter of taste. However, beware that the pointer actually always binds to the name, not the type. So this:

Type* var1, var2;

Declares var1 as a pointer to Type, while var2 is not a pointer. That’s just one more reason not to declare multiple variables in the same statement.

Historically, the Type *var notation is more common in C, where it is read as “var is declared as a pointer to Type”, i.e. “the type of *var is Type”. In C++, on the other hand, Type* var is more common and is read as “var is declared as being of type ‘pointer to Type’”.


There is no difference at all.

But consider the following line:

Type* var1, var2;

Here only var1 is a pointer to Type, but it is easier to see that if you write

Type *var1, var2; 


Are you sure it acts differently ? It seems a bit strange to me. Even if I don't know about Objective-c, in C there would be no difference between

Type* var
Type * var
Type *var

It's just a matter of syntax / preference.

Except if you're not talking about declaring a variable, but access it ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜