静态的不同语言 [英] static in different languages
本文介绍了静态的不同语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说过语言之间关于关键字 static
的含义存在差异,但是我没有找到一个很好的列表来合并这些差异.
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.
这是我所知道的C ++中 static
的含义:
Here's what I know about the meaning of static
in C++:
- 对于函数中的局部静态变量,该变量在启动时初始化,并且值在函数调用之间保存.
- 静态数据成员在类的所有实例之间共享.换句话说,只有一个静态数据成员的实例.静态数据成员必须在文件范围内初始化.
- 静态成员函数只能访问静态成员.
- 在递归代码中,保证静态对象或变量在代码块的不同实例中具有相同的状态.
- 在文件作用域定义的静态对象和变量仅具有内部链接.没有其他文件可以使用它们.
static
的含义在其他语言中有何变化?
How does the meaning of static
change in other languages?
推荐答案
C
- 关键字可以更改对象的链接或持续时间(生命周期).
- 变量始终初始化为0
- 功能具有内部链接.
- 如果在文件级范围内声明:变量具有内部链接和静态持续时间(即在程序的整个生命周期中都存在)
- 如果在块范围内声明:变量没有链接,但静态持续时间
- 翻译单元中可以有多个相同静态变量的声明.但是,请注意它们必须相同.例如:在文件级范围内:
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 ++
- 在文件级别范围内,不赞成使用变量和成员,而建议使用匿名名称空间.仅作为兼容性存在
- 变量仍默认初始化为0(与C中一样)
- "6.7在执行任何其他初始化之前,先执行具有静态存储持续时间(3.7.1)或线程存储持续时间(3.7.2)的所有本地对象的零初始化(8.5)[...] "
- 除非具有
thread_local
说明符(从C ++ 0x开始),否则变量具有静态存储期限 - 翻译单元中只能有一个静态的定义
- 成员变量/函数表示它们是类的属性,而不是实例的属性 合法访问语法:instance.property或Class :: property
- 静态成员函数只能访问静态成员变量
此类功能没有
this
指针 - 但是非静态成员可以访问任何静态成员
- 在文件级,对象具有内部链接,但具有类作用域的类成员除外
- 需要通过类名和作用域解析运算符在类声明中或在外部明确定义类成员
- 不能在静态方法中使用
this
- 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
- C ++中的类方法
- 不能在静态方法中使用
this
或super
- 仅通过类名而不是实例名访问
- 未继承
- 但是派生的类可以访问基础的静态属性
- 使用static和const关键字声明的变量必须在声明常量的同时初始化
- Class methods as in C++
- cannot use
this
orsuper
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
面向对象的设计
- Singleton设计模式被许多人视为美化的静态对象
- 用于工厂设计模式
我可能已经错过了许多其他事情-随时随地参加.
I may have missed a lot of other things -- feel free to chip in.
这篇关于静态的不同语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文