static和const变量有什么区别?

jai*_*min 62 c c++ static const

有人可以解释一个staticconst变量之间的区别吗?

Ste*_*all 41

常数值不能改变.静态变量存在于函数或类中,而不是实例或对象.

这两个概念并不相互排斥,可以一起使用.


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++的细微差别.看看书或标准.

  • @litb:因为,不允许函数的内联定义在C99中具有可修改的静态对象定义.见6.7.4.第3段和下一页(可能)的相关脚注. (3认同)

use*_*970 8

静态变量:

  • 仅初始化一次.
  • 静态变量适用于类(不是每个对象).即每个类只分配一次内存,每个实例都使用它.因此,如果一个对象修改了它的值,那么修改后的值也可以被其他对象看到.(一个简单的想法..要知道为类创建的对象的数量,我们可以放置一个静态变量并在构造函数中执行++)
  • 不同函数调用之间的值仍然存在

Const变量:

  • Const变量是一种承诺,您不会在程序中的任何位置更改其值.如果你这样做,它会抱怨.


Thu*_*rGr 7

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++ 这样的语言中,声明静态全局变量是没有意义的,但它们在函数和类中非常有用。在托管语言中,具有全局变量效果的唯一方法是将其声明为静态。


H. *_*een 5

常量不能更改,静态变量更多地与它们的分配方式和可访问位置有关.

看看这个网站.


Par*_*mar 5

static is a storage specifier.
const is a type qualifier.
Run Code Online (Sandbox Code Playgroud)