關於靜態變數

2021-06-18 16:19:57 字數 1402 閱讀 4872

靜態變數的型別關鍵字是static,其包括靜態全域性變數和靜態區域性變數。靜態區域性變數的作用域是區域性的,即存在於乙個檔案或者乙個函式中,而靜態全域性變數則存在於整個函式中,即當乙個源程式由多個原始檔組成時,非靜態的全域性變數在各個原始檔中都是有效的。。

靜態區域性變數和動態區域性變數

3

4

5

6

7

8

9

10

11

12

13

#include

intfun(intn)

voidmain()

這裡的執行結果是:?

12

3

4

5

fun(1) = 1

fun(2) = 2

fun(3) = 6

fun(4) = 24

fun(5) = 120

這裡的f因為是靜態區域性變數,因此f每次在呼叫以後,其值仍然是有效的。

動態區域性變數 2

3

4

5

6

7

8

9

10

11

12

13

#include

intfun(intn)

voidmain()

程式的執行結果是:?1

2

3

4

5

fun(1) = 1

fun(2) = 2

fun(2) = 3

fun(4) = 4

fun(5) = 5

關於靜態變數

對於函式來說,定義與宣告很容易區別,但對於變數來說則不是,為了辨明,試驗了一下幾段 include using namespace std class test2 int test2 i int main int argc,const char argv 這段 可以通過 此時主函式中未使用類test...

靜態變數和非靜態變數

class string1 a和b編譯報錯,can not make a static referrence to no static filed s3,s4 分析 string s1 pingguo string s2 xiangjiao string s3 new string lizi str...

關於靜態變數的繼承

父類 package test public class father 子類1 package test public class son1 extends father 子類2 package test public class son2 extends father 測試 package tes...