某种外部变量和静态变量之间的区别
某种外部变量和静态变量有什么区别?
//ClassA.m
NSString *var1;
static NSString *var2;
@implementation ClassA
...
What the difference between external variable of some sort and static variable?
//ClassA.m
NSString *var1;
static NSString *var2;
@implementation ClassA
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
extern
extern
常量/变量是一种可以跨多个对象文件访问(或引用)其定义的常量/变量。它是一个全局的、导出的 C 符号。如果您想从多个翻译单元(或编译文件)访问常量/全局变量,或者您想在多个二进制文件中使用它(例如,您想从应用程序中使用它,并且定义位于动态库中),请使用此变量)。您通常会在标头中声明它以供其他人使用。static
static
为每个翻译发出一个副本。每个编译后也看到(例如#include)静态文件的文件都会发出该静态文件的副本。你应该避免这种情况。这将导致二进制文件臃肿,执行过程非常混乱。如果该值是文件的本地值,并且应该是该文件的私有值,那么您应该倾向于使用静态值。由于这些原因,您应该在 .c、.m、.cpp 和 .mm 文件中使用
static
,并在标头中使用extern
。最后,默认情况下,
NSString
指针应该是const
:或者
如果您还想要更多,这是我写的相关答案,以及这是另一个。
extern
An
extern
constant/variable is one which one definition may be accessed (or referenced) across multiple object files. It's a global, exported C symbol. Use this if you want access to the constant/global variable from multiple translation units (or compiled files) or if you want to use it in multiple binaries (e.g. you want to use it from your app, and the definition is in a dynamic library). You will typically declare this in the header for others to use.static
The
static
emits a copy for each translation. Each file that is compiled which also sees (e.g. is#include
d) the static will emit a copy of that static. You should avoid this. This will result in a bloated binary that has very confusing execution. You should favor the static if the value is local to a file, and should be private to that file.For these reasons, you should favor
static
in your .c, .m, .cpp, and .mm files, andextern
in your headers.Finally, that
NSString
pointer should beconst
by default:or
If you still want more, here's a related answer I wrote, and here's another.
全局变量和
静态
在任何方法之外声明变量(在 Objective-C 中,
@implementation
/内部或外部) @end
使变量的生命周期成为全局变量,即该变量将在应用程序执行的整个时间段内存在。没有限定或仅带有
的全局变量。 const
和/或易失性
限定符,在任何地方都是可见的,例如:声明一个可以从应用程序中的任何位置访问的全局变量
(但不是) 。通过使用
static
对其进行限定,可以将其限制为仅包含声明的文件(更准确地说是“编译单元”,允许一个文件包括其他文件),例如:声明一个全局变量
。 >MyClassName
这只是从当前编译单元可见,在
@implementation
/@end
中使用static
限定的全局声明是 Objective-C 提供的与其他语言最接近的功能。 '“类变量”。您不应该将全局变量声明(无论
静态
限定与否)放在头文件中,因为这会导致多个不同的声明 - 如果没有,则会导致错误static
限定符,以及具有不同可见范围的多个不同变量(如果有)。extern
限定符extern
限定符的作用相当不同。最初的含义(现在允许稍微放宽,见下文)是仅定义在其他地方声明的全局变量的类型和名称。也就是说,您告诉编译器“有一个我想使用的某种类型和名称的全局变量,它将由另一个编译单元提供”。例如:
声明某些编译单元包含声明(不得是
static
限定的):extern
限定的声明本身不会导致为全局分配任何存储空间。多变的。您确实将
extern
限定声明放入头文件中,这就是编译单元通告其希望导出的全局变量的方式。您还可以将它们放在代码文件中以引用其他地方声明的全局变量。在代码文件中,您可以在任何方法/函数之外放置extern
限定声明 - 在这种情况下,名称/类型可用于整个编译单元 - 或在方法/函数内 - 在这种情况下在这种情况下,名称/类型的可见性仅限于该方法/函数。小小的放松
最初,您必须有一个非
extern
限定符声明才能满足任何extern
限定符声明。然而,这后来被改变了,如果将各个编译单元链接在一起,它们都没有声明由某些extern
限定声明引用的全局变量,那么链接器(通常?)只会生成变量本身并将其添加到生成的二进制文件,而不是产生“丢失全局”错误。最佳编程实践不是依赖这种放松 - 始终为每个全局变量提供非extern
限定声明。Global variables and
static
Declaring a variable outside of any method (and in Objective-C either within, or outside,
@implementation
/@end
makes the lifetime of the variable global, that is the variable will exist during the whole time period the application is executing.A global variable with no qualification, or only with
const
and/orvolatile
qualifiers, is visible from anywhere. So for example:declares a global variable
MyApplicationName
which can be accessed from any place in the application.The visibility of a global variable (but not its lifetime) can be restricted to just the file (more accurately "compilation unit" allowing for one file including others) containing the declaration by qualifying it with
static
. So for example:declares a global variable
MyClassName
which is only visible from the current compilation unit.Using
static
qualified global declarations within@implementation
/@end
is the closest thing Objective-C offers to other languages' "class variables".You should not place global variable declarations,
static
qualified or not, in header files as that would result in multiple distinct declarations - an error will result if there is nostatic
qualifier, and multiple distinct variables with different visibility scopes if there is one.The
extern
qualifierThe
extern
qualifier does something rather different.The original meaning (a small relaxation is now allowed, see below) is to just define the type and name of a global variable which is declared elsewhere. That is you are telling the compiler "there is a global variable of some type and name I'd like to use which will be supplied by another compilation unit". For example:
states that some compilation unit contains the declaration (which must not be
static
qualified):The
extern
qualified declaration does not itself cause the allocation of any storage for the global variable.You do place
extern
qualified declarations in header files, this is how a compilation unit advertises the global variables it wishes to export. You can also place them in code files to reference globals variables declared elsewhere. Within a code file you can place anextern
qualified declaration either outside of any method/function - in which case the name/type is available for to whole compilation unit - or within a method/function - in which case the visibility of the name/type is limited to just that method/function.The small relaxation
Originally you had to have one non-
extern
qualifier declaration to satisfy anyextern
qualified ones. However this was later changed, if on linking the various compilation units together none of them declare a global variable referenced by someextern
qualified declaration then the linker (usually?) just generates the variable itself and adds it to the resultant binary rather than producing a "missing global" error. Best programming practice is not to rely on this relaxation - always have a none-extern
qualified declaration for every global variable.我可以给出一个解释,但它不会像这个那么好:
http://blog.ablepear.com/2010/01/objective-c-tuesdays-static-variables.html
I could give an explanation, but it wouldn't be as good as this one:
http://blog.ablepear.com/2010/01/objective-c-tuesdays-static-variables.html