static关键字(1)

【1】全局、局部静态变量的特点

通过代码分析如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 static int MAX = 10;
 5 // MAX += 100;   // error! 全局静态变量不可以直接在全局参与运算
 6 
 7 void fun(int nValue)
 8 {
 9     static int x = nValue;    // 局部静态变量
10     int b = 10;          // 一般变量
11     ++x;
12     cout << &x << "  " << x << endl;
13     cout << &b << "    " << b << endl;
14 }
15 
16 int show()
17 {
18     // x = 100;   // error!不可以改变静态局部变量的值(可见性问题)
19 
20     return MAX += 10;
21 }
22 
23 void main()
24 {
25     cout << show() << endl;
26     cout << &MAX << " " << MAX <<endl;
27     for (int i = 0; i < 10; ++i)
28     {
29         fun(i);
30     }
31     system("pause");
32 }
33 
34 //运行结果如下:
35 /*
36 20
37 00F89000 20
38 00F8916C  1
39 0022F6F8    10
40 00F8916C  2
41 0022F6F8    10
42 00F8916C  3
43 0022F6F8    10
44 00F8916C  4
45 0022F6F8    10
46 00F8916C  5
47 0022F6F8    10
48 00F8916C  6
49 0022F6F8    10
50 00F8916C  7
51 0022F6F8    10
52 00F8916C  8
53 0022F6F8    10
54 00F8916C  9
55 0022F6F8    10
56 00F8916C  10
57 0022F6F8    10
58 请按任意键继续. . .
59 */

总结:

(1)局部静态变量只能在函数内部(内部即局部)。值只进行一次初始化,以后每次再执行函数时保持上一次执行过的值。

(2)全局静态变量是在全局变量前加一个static,使该变量只在这个源文件中可用,称之为全局静态变量。全局静态变量即静态全局变量。

  在多个文件组成的程序里,全局变量与全局静态变量的区别:全局静态变量使得该变量为定义该变量的源文件所独享,其他文件不能使用。  

  全局静态变量不可以在全局进行运算操作。

【2】解释形参,局部变量和静态局部变量的差别?

答案:从本质上说,三者均属于局部作用域中的变量,其中局部变量又可区分为普通局部变量和静态局部变量,

它们的差别在于:

(1)形参的作用域为整个函数体,而普通变量和静态局部变量的作用域为:从定义处到包含该变量定义的块的结束处

(2)形参由调用函数时所传递的实参初始化,而普通局部变量和静态局部变量通常用初始化式进行初始化,且均在程

序执行流程第一次经过该对象的定义语句时进行初始化。静态局变量的初始化在整个程序执行过程中只执行一次。

(3)形参和普通局部变量均属于自动变量,在每次调用函数是创建,并在函数结束时撤销,而静态局部变量的生命期

却跨越了函数的多次调用,并在创建后直接到程序结束时才撤销。

【3】静态成员变量有何特点?

静态成员变量只初始化一次,即第一次执行赋值。

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int fun(int a)
 5 {
 6     static int x = a;
 7     x += 10; 
 8     cout << x << "  ";
 9     return x;
10 }
11 
12 void main()
13 {
14     for (int i = 10; i > 0; --i)
15     {
16         fun(i);  
17     }
18     system("pause");
19 }
20 // run out:
21 // 20  30  40  50  60  70  80  90  100  110  请按任意键继续. . .
22 // 说明:局部静态变量有且仅有第一次初始化。

【4】通过引用如何改变静态局部变量的值?

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int & fun(int a)
 5 {
 6     static int x = a;
 7     x += 10;
 8     cout<<x<<" ";    
 9     return x;
10 }
11 void  main()
12 {
13     for(int i = 0; i < 10; i++)
14     {
15         int &y = fun(i);
16         y += 100;
17     }
18 }
19 //运行结果如下:
20 //10 120 230 340 450 560 670 780 890 1000
21 //说明:引用返回生存期不受函数限制

【5】通过指针如何改变静态局部变量的值

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int *fun(int a)
 5 {
 6     static int x = a;
 7     x += 10;
 8     cout<<x<<" ";
 9     return &x;
10 }
11 
12 void  main()
13 {
14     for(int i = 0; i < 10; i++)
15     {
16         int *p = fun(i);
17         *p = 100+i;
18     }
19 }
20 //运行结果如下:
21 //10 110 111 112 113 114 115 116 117 118 
22 //说明:指针返回生存期不受函数限制

【6】局部变量的生存期如何分析

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 public:
 7     Test()
 8     {
 9         cout<<"Create Test: "<<endl;
10     }
11     ~Test()
12     {
13         cout<<"Free Test: "<<endl;
14     }
15 };
16 
17 void fun()
18 {
19     Test t1;
20 }
21 
22 void main()
23 {
24     for(int i = 0; i < 5; ++i)
25     {
26         fun();
27     }
28     cout<<"main end"<<endl;
29 }
30 //输出结果如下:“十个 创建和析构 对象”
31 /*
32 Create Test:
33 Free Test:
34 Create Test:
35 Free Test:
36 Create Test:
37 Free Test:
38 Create Test:
39 Free Test:
40 Create Test:
41 Free Test:
42 main end
43 */
44 //说明:局部变量的生存期仅仅在块内

局部变量的生存期仅仅在块内。

【7】静态成员在全局区域开辟的空间,生存期不受函数限制如何分析?

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 public:
 7     Test()
 8     {
 9         cout<<"Create Test: "<<endl;
10     }
11     ~Test()
12     {
13         cout<<"Free Test: "<<endl;
14     }
15 };
16 
17 void fun()
18 { 
19     static Test t1;
20 }
21 void main()
22 {
23     for(int i = 0; i < 10; ++i)
24     {
25         fun();
26     }
27     cout<<"main end"<<endl;
28 }
29 //输出结果如下:
30 /*
31     Creat  Test
32     main  end
33     Free   Test
34 */
35 //说明:静态成员在全局区域开辟的空间,生存期不受函数限制

【8】类内静态成员变量必须在类外初始化,并且遵循访问属性原则举例?

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 //private:  //注意访问权限不同的区别
 7 public:
 8     int value;
 9     static int num;   //静态类的成员变量
10 public:
11     Test(int x=0):value(x)
12     {
13     }
14     ~Test()
15     {}
16 };
17 int Test::num=0;  //必须在类外初始化
18 void main()
19 {
20     Test t1(10), t2(20), t3(30);
21     
22     cout<<t1.num<<endl;
23     t1.num += 100;
24     cout<<Test::num<<endl;
25 }
26 //运行结果如下:
27 /*
28 0
29 100
30 */
31 //说明:类内静态成员变量必须在类外初始化,并且遵循访问属性原则

【9】静态成员变量的计数作用如何体现?

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 private:
 7     int value;
 8     static int num;
 9 public:
10     Test(int x=0):value(x)
11     {
12         cout<<"Create Test: "<<++num<<endl;
13     }
14     ~Test()
15     {
16         cout<<"Destroy Test: "<<--num<<endl;
17     }
18 };
19 
20 int Test::num=0;
21 void main()
22 {
23     Test t1[10];   //创建十个对象,用num进行计数
24 }
25 
26 //运行结果如下:
27 /*
28 Create Test: 1
29 Create Test: 2
30 Create Test: 3
31 Create Test: 4
32 Create Test: 5
33 Create Test: 6
34 Create Test: 7
35 Create Test: 8
36 Create Test: 9
37 Create Test: 10
38 Destroy Test: 9
39 Destroy Test: 8
40 Destroy Test: 7
41 Destroy Test: 6
42  Destroy Test: 5
43 Destroy Test: 4
44 Destroy Test: 3
45 Destroy Test: 2
46 Destroy Test: 1
47 Destroy Test: 0
48 */

【10】类中静态成员的访问权限如何分析

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 #include<iostream.h>
 5 class Test
 6 {
 7 public:
 8     int value;
 9     static int num;
10 public:
11     Test(int x=0):value(x)
12     {
13         cout<<"Create Test: "<<++num<<endl;
14     }
15     ~Test()
16     {
17         cout<<"Destroy Test: "<<--num<<endl;
18     }
19     
20     void fun()
21     // == void fun(Test * const this) //注意const的位置
22     {
23         this->value+=10;
24         cout<<num<<endl;
25     }
26     //static void sfunc()
27     static void sfunc(Test &t) //静态成员函数没有this指针
28     {   // no this  pointer;
29         //value += 10;        //error!!!因为静态成员函数没有this指针,
30         t.value+=10;
31         cout<<num<<endl;
32     }
33 };
34 int Test::num=0;  //初始化静态成员变量
35 void main()
36 {
37     Test t1(10),t2(10);
38     t1.fun();
39     t1.sfunc(t1);
40     
41     Test::sfunc(t2);
42     
43     Test::num = 10; //公共变量可以访问,私有不可
44     
45 }
46 //运行结果如下:
47 /*
48 Create Test: 1
49 Create Test: 2
50 2
51 2
52 2
53 Destroy Test: 9
54 Destroy Test: 8
55 */

【11】类的静态数据成员~常量成员~静态常量成员各有何区别

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 private:
 7     int value;
 8     static int num;
 9     const int max;
10     static const int min;   //=0;// vs2005可以直接初始化  vC6.0  error!!!!!
11 public:
12     Test(int x=0):value(x),max(x+20)  //类中const数据成员变量采用参数列表进行初始化 
13     {
14         // == value = x;
15         // == max = x+20;
16     }
17     ~Test()
18     {
19     }
20 };
21 
22 int Test::num=10;       //静态成员变量在类外初始化
23 const int Test::min=0;  //静态常量成员在类外初始化
24 
25 void main()
26 {
27     Test t1(10),t2(20);
28 }

【12】静态成员函数~一般成员函数~常成员函数之间的访问权限如何?

示例代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Test
 5 {
 6 public:
 7     int value;
 8     static int num;
 9 
10 public:
11     Test(int x = 0):value(x)
12     {}
13     ~Test()
14     {}
15 public:
16     void func()
17     // void func(Test * const this)
18     {
19         cfunc(); //sfunc(this);//相当于限制了this的能力
20         cout<<value++<<endl;
21         cout<<num++<<endl;
22     }
23     void cfunc() const
24     // void sfunc(const Test * const this)
25     {
26         //func();// func(this);  //error! 相当于扩大了this的能力
27         this->value;             //成员变量不可以变量
28         //cout<<value++<<endl;   //error! const修饰限定对象的成员不被改变
29         cout<<num++<<endl;     //OK  因为num为静态成员变量
30         sfun();
31     }
32     static void sfun() //const
33     {  // no this
34         //func();
35     }
36 };
37 
38 int Test::num=0;
39 
40 void main()
41 {
42     Test t1(10);
43     t1.func();
44     //t1.sfunc(); //error!!!sfunc()为静态成员函数  不可以通过对象直接调用
45 }

  

作者:kaizen
声明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此声明,且在文章明显位置给出本文链接,否则保留追究法律责任的权利。
签名:顺序 选择 循环
原文地址:https://www.cnblogs.com/Braveliu/p/2840473.html