Objective-c 语法 - Class *var 与 Class* var
我有 PHP 背景,而且是 Objective C 的新手。 我在 var 将其标识为指针之前得到一个星号。但是,类名后面的星号是什么意思?我已经看到了这两种方式,这不是一个错误。风格差异是否具有相同含义?
Class *var
Class* var
I'm from a PHP background and I'm new to Objective C.
I get that an asterisk before a var identifies it as a pointer. However what does an asterisk AFTER a CLASS NAME mean? I've seen it both ways and it's not a mistake. Is a style difference with the same meaning?
Class *var
Class* var
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这两个语句在语义上是等效的。这是一个偏好问题。需要注意的一件事是:
该语句声明 var1 为
Class*
,但 var2 为Class
。将它们声明为指向类的指针:这就是为什么我更喜欢将星号分组在变量名附近,即使它对于您声明的类型不太“正确”。
The two statements are semantically equivalent. It's a matter of preference. One thing to note:
That statement declares var1 to be
Class*
but var2Class
. To declare both of them a pointer to Class:Which is why I prefer grouping the asterisk near the variable name, even though it's less "correct" to the type you're declaring.
就像 C 和 C++ 一样,它也有风格。
Just like C and C++, it's stylistic.
在这种情况下,空格并不重要,因此您可以任意编写。
The whitespace is not significant in this case so you can write either.
一样的意思。在声明多个变量的情况下,在类名后面使用星号会有些误导;例如,在这一行中:
星号仅适用于 a。但有些人认为 * 是数据类型的一部分,因此与类名属于一起。
Same meaning. Using the asterisk after the class name is somewhat misleading in case of multiple variable declarations; for example, in this line:
the asterisk only applies to a. But some people argue that * is a part of the datatype, therefore belongs together with the class name.