iphone - static T const 和 static const T 的区别

标签 iphone objective-c c ios

假设 T 是任意类型,我们正在谈论 Objective-C。那有什么区别呢

static T const x = ....

static const T x = ....

?

最佳答案

它们是一样的。


关于 const 的一些背景知识:

Objective-C,就像 C,使用 Clockwise/Spiral Rule .在变量(foo)的左侧只有修饰符的情况下,您会从右向左读取内容:

int * const * var;

读作:var 是指向整型常量指针的指针

然而 const 在第一种类型之前或之后具有相同的效果:

int const var; // constant integer
const int var; // constant integer, same as above

相反,

static 会影响整个变量:变量是静态的,而不是它指向的元素或其他任何东西:指向整数的指针可以同时用于指向静态整数, 或自动整数而没有区别,并且 C 不提供语法来声明仅能指向静态变量(或自动变量)的指针。

static int * x; // x is a static pointer to integer
int static * y; // as above y is a static pointer to integer
int * static z; // THIS SYNTAX IS NOT ALLOWED
                // but if it was, it would mean the same thing

出于这个原因,我更喜欢将它放在任何 const 修饰符之前,但这实际上并不重要。

static  const int * const * x; // this is what I do

关于iphone - static T const 和 static const T 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4666435/

相关文章:

c - 将空字符插入 snprintf

iphone - 将 iphone 应用程序转换为 iPad

iphone - 在 ios 中自由手绘多段线叠加

ios - 从另一个 watch 应用程序中启动 Apple Watch 消息应用程序

iphone - 如何增加iPhone中UITabBarItem的大小?

c - 初学c,工程不编译?

完成循环的 c int 比较

iphone - 防止objc block 复制属性

ios - 使用 Storyboard xcode6 在 View Controller 之间传递数据

objective-c - 离开应用程序后,NSUserDefaults将不会加载