问题描述
如果在方法中创建本地静态变量,那么每个实例都初始化一次,或者每个程序初始化一次。
If you create a local static variable inside a method, is that initialised once per instance, or once per program?
这在C ++和Objective-C ?
Does this differ between C++ and Objective-C?
推荐答案
即使是每个节目一次,每个节目一次
Once per program.
它是在非静态类成员函数中,它不与任何类实例相关联;在整个程序中只有一个变量实例,只初始化一次。
Even if it is in a non-static class member function, it is not associated with any class instance; there will only be one instance of the variable in the whole program, initialised just once.
在C ++中,它在第一次调用函数时初始化。在C(和Objective-C)中,它在程序启动之前被初始化。在实践中,这没有什么区别,因为初始化在C中不会有任何副作用。
In C++, it is initialised the first time the function is called. In C (and Objective-C), it is initialised prior to program startup. In practice, this doesn't make a difference, since the initialisation can't have any side effects in C.
这篇关于方法调用中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!