可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've heard there are differences between languages about the meaning of the keyword static
, but I've not found a good list that consolidates those differences.
Here's what I know about the meaning of static
in C++:
- For local static variables within a function, the variable is initialized at startup and the value is saved across function calls.
- Static data members are shared among all instances of a class. In other words, there is only one instance of a static data member. Static data members must be initialized at file scope.
- Static member functions have access only to static members.
- In recursive code, a static object or variable is guaranteed to have the same state in different instances of a block of code.
- Static objects and variables defined at file scope only have internal linkage. No other files may use them.
How does the meaning of static
change in other languages?
回答1:
C
- The keyword can change either the linkage or the duration (lifetime) of an object.
- Variables are always initialized to 0
- Functions have internal linkage.
- If declared in file level scope: variables have internal linkage and static duration (i.e. exists throughout the lifetime of the program)
- If declared in block scope: variables have no linkage but static duration
- There can multiple declarations of the same static variable in a translation unit. However, note that they must be the same. E.g: at file-level scope:
int a; // a has external linkage
static int a; // a now has static linkage
// same as if you wrote: static int a = 0;
//...
static int b; // static linkage
extern int b; // extern loses its meaning, b still has internal linkage
//...
extern int b; // b has external linkage
static int b; // error
//...
void func() {
static int x; // automatic linkage, static duration
// same as if you wrote: static int x = 0;
}
C++
- At file level scope the usage has been deprecated for both variables and members in favor of anonymous namespaces. Exists only as compatibility
- Variables still get default initialized (as in C) to 0
- "6.7 The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place [...] "
- Variables have static storage duration unless accompanied by a
thread_local
specifier (from C++0x onwards)
- There can be only one definition of a static in a translation unit
- Member variables/functions mean they are properties of the class and not the instances
Legal access syntax: instance.property or Class::property
- Static member functions can only access only static member variables
No
this
pointer for such functions
- Non-static members can however access any static member
- At file level objects have internal linkage except for class members which have a class scope
- Class members need to be defined either in the class declaration or outside explicitly via class name and scope resolution operator
- Cannot use
this
in a static method
ActionScript
- Class methods as in C++
- cannot use
this
or super
in a static method
- Accessed only through class name and not instance name
- Not inherited
- Derived classes however have access to bases' static properties
- Variables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant
Object Oriented Design
- The Singleton design pattern is considered by many as a glorified static object
- Used in Factory design pattern
I may have missed a lot of other things -- feel free to chip in.
回答2:
In Delphi the static keyword is used exclusively for defining class methods. In Delphi a normal class method can be declared virtual and overridden in a subclass. Additionally Delphi has a self variable, similar to the this pointer in other languages. However in a class method the self points to the class in which the method is called instead of an instance.
Declaring a class method static means:
- It cannot be overridden in a subclass
- It does not have a self pointer
This means a static class method can only access class members in the class it was defined in, while a normal class method can access overridden class members in derived classes.
There are other informal uses of static in the Delphi documentation usually referring to a feature unchangability (is that a word?). For instance a static array vs a dynamic array. All instance methods in Delphi are static unless declared otherwise.
回答3:
In VB.NET, a Static
variable is just like a C++ local static variable.
However, there is no class-wide Static
; use Shared
instead.
回答4:
In C# there are 3 ways a static keyword can be used:
- On class definition, which means the class will only expose static members and cannot be instanciated
- On a class member, which means that the member can be called without having to instanciate the class.
- On a constructor, which means that the static constructor will allways be called before a static member is called. (Those are mostly performance bottlenecks and therefore not recommended)
Hope this helps.
回答5:
In C# it pretty much always means: "related to a type rather than an instance of the type".
回答6:
Python has the decorator @staticmethod, which when applied to a class member, makes the method available on the class rather than instances, and passes no automatic arguments to the method. The @classmethod decorator performs a similar function, but passes the class as the first argument, making it much more useful.
回答7:
In C, static flags a function or global variable as local to the file its located in.
It's kinda like private in other languages. Sorta.
If it's in a function, then static preallocates that variable in the data section of the binary, rather than on the stack at run time.
回答8:
In VB.NET static is used procedure level to mean the variable is associated with all executions of the procedure (it's preserved from call to call). That's a bit of an arcane usage in an Object-Oriented application though.
The counterpart is "Shared" which means the method or member is type level (you don't need an instance to access it).
回答9:
Wikipedia summarizes many different meanings of static
:
Static Methods, static variables, static typing.