dir*_*tly 25
简短的回答:
A const是一种承诺,一旦设置,您将不会尝试修改该值.
甲static变量意味着该对象的生存期是程序的整个执行和它的程序启动前值被初始化仅仅一次.如果没有为它们显式设置值,则初始化所有静态.静态初始化的方式和时间未指定.
C99借用了constC++.另一方面,static由于其经常令人困惑的语义,一直是许多辩论(两种语言)的来源.
此外,对于C++ 0x直到C++ 11,static不推荐使用该关键字来声明命名空间范围内的对象.由于各种原因,在C++ 11中删除了此弃用(请参阅此处).
更长的答案:更多关于关键字的信息比你想知道的更多(从标准来看):
C99
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
/* file scope, static storage, internal linkage */
static int i1; // tentative definition, internal linkage
extern int i1; // tentative definition, internal linkage
int i2; // external linkage, automatic duration (effectively lifetime of program)
int *p = (int []){2, 4}; // unnamed array has static storage
/* effect on string literals */
char *s = "/tmp/fileXXXXXX"; // static storage always, may not be modifiable
char *p = (char []){"/tmp/fileXXXXXX"}; // static, modifiable
const char *cp = (const char []){"/tmp/fileXXXXXX"} // static, non-modifiable
void f(int m)
{
static int vla[ m ]; // err
float w[] = { 0.0/0.0 }; // raises an exception
/* block scope, static storage, no-linkage */
static float x = 0.0/0.0; // does not raise an exception
/* ... */
/* effect on string literals */
char *s = "/tmp/fileXXXXXX"; // static storage always, may not be modifiable
char *p = (char []){"/tmp/fileXXXXXX"}; // automatic storage, modifiable
const char *cp = (const char []){"/tmp/fileXXXXXX"} // automatic storage, non-modifiable
}
inline void bar(void)
{
const static int x = 42; // ok
// Note: Since an inline definition is distinct from the
// corresponding external definition and from any other
// corresponding inline definitions in other translation
// units, all corresponding objects with static storage
// duration are also distinct in each of the definitions
static int y = -42; // error, inline function definition
}
// the last declaration also specifies that the argument
// corresponding to a in any call to f must be a non-null
// pointer to the first of at least three arrays of 5 doubles
void f(double a[static 3][5]);
static void g(void); // internal linkage
Run Code Online (Sandbox Code Playgroud)
C++
具有相同的语义,除非在简短的回答中指出.此外,没有参数限定statics.
extern "C" {
static void f4(); // the name of the function f4 has
// internal linkage (not C language
// linkage) and the function’s type
// has C language linkage.
}
class S {
mutable static int i; // err
mutable static int j; // err
static int k; // ok, all instances share the same member
};
inline void bar(void)
{
const static int x = 42; // ok
static int y = -42; // ok
}
Run Code Online (Sandbox Code Playgroud)
static我在这里省略了一些C++的细微差别.看看书或标准.
静态变量:
Const变量:
const等效于#define但仅适用于值语句(例如#define myvalue = 2)。声明的值在编译之前替换变量的名称。
static是一个变量。该值可以更改,但即使在函数中声明了该变量,该变量也会在整个程序执行过程中保持不变。它相当于一个全局变量,其使用范围是声明它们的块的范围,但其值的范围是全局的。
因此,静态变量只初始化一次。如果变量是在函数中声明的,这一点尤其重要,因为它保证初始化只会在第一次调用函数时发生。
静态的另一种用法涉及对象。在对象中声明静态变量的效果是该值对于该对象的所有实例都是相同的。因此,不能使用对象的名称调用它,而只能使用类的名称。
public class Test
{
public static int test;
}
Test myTestObject=new Test();
myTestObject.test=2;//ERROR
Test.test=2;//Correct
Run Code Online (Sandbox Code Playgroud)
在像 C 和 C++ 这样的语言中,声明静态全局变量是没有意义的,但它们在函数和类中非常有用。在托管语言中,具有全局变量效果的唯一方法是将其声明为静态。