【C++快速上手】三、static学习笔记

网友投稿 823 2022-05-30

结论

类中的静态变量由对象共享。对于不同的对象,不能有相同静态变量的多个副本。也是因为这个原因,静态变量不能使用构造函数初始化。类中的静态变量应由用户使用类外的类名和范围解析运算符显式初始化,如:int Apple::i = 1;。

类对象为静态时,静态对象的范围是贯穿程序的生命周期。否之,只在其定义的范围内。

当与不同类型一起使用时,Static关键字具有不同的含义,常用的用法有以下两种:

1、静态变量:函数中的变量,类中的变量

2、静态类的成员(静态成员):类对象和类中的函数

1、静态变量

1.1、函数中的静态变量

【这个情况经常用,应该很熟悉了】

当变量声明为static时,空间将在程序的生命周期内分配。即使多次调用该函数,静态变量的空间也只分配一次,前一次调用中的变量值通过下一次函数调用传递。这对于在C / C ++或需要存储先前函数状态的任何其他应用程序非常有用。

#include #include using namespace std; void demo() { // static variable static int count = 0; cout << count << " "; // value is updated and // will be carried to next // function calls count++; } int main() { for (int i=0; i<5; i++) demo(); return 0; } // 输出 0 1 2 3 4

1

2

【C++快速上手】三、static学习笔记

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

上面的程序中可以看到变量count被声明为static。因此,它的值通过函数调用来传递。每次调用函数时,都不会对变量计数进行初始化。

1.2、类中的静态变量

由于声明为static的变量只被初始化一次,因为它们在单独的静态存储中分配了空间,因此类中的静态变量由对象共享。对于不同的对象,不能有相同静态变量的多个副本。也是因为这个原因,静态变量不能使用构造函数初始化。

#include using namespace std; class Apple { public: static int i; Apple() { // Do nothing }; }; int main() { Apple obj1; Apple obj2; obj1.i = 2; obj2.i = 3; // prints value of i cout << obj1.i<<" "<

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

您可以在上面的程序中看到我们已经尝试为多个对象创建静态变量i的多个副本。但这并没有发生。因此,类中的静态变量应由用户使用类外的类名和范围解析运算符显式初始化,如下所示:

#include using namespace std; class Apple { public: static int i; Apple() { // Do nothing }; }; int Apple::i = 1; int main() { Apple obj; // prints value of i cout << obj.i; } //输出 1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

2、静态类的成员(静态成员)

2.1、类对象为静态

就像变量一样,对象也在声明为static时具有范围,直到程序的生命周期。考虑以下程序,其中对象是非静态的。

#include using namespace std; class Apple { int i; public: Apple() { i = 0; cout << "Inside Constructor\n"; } ~Apple() { cout << "Inside Destructor\n"; } }; int main() { int x = 0; if (x==0) { Apple obj; } cout << "End of main\n"; } //输出 Inside Constructor Inside Destructor End of main

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

在上面的程序中,对象在if块内声明为非静态。因此,变量的范围仅在if块内。因此,当创建对象时,将调用构造函数,并且在if块的控制权越过析构函数的同时调用,因为对象的范围仅在声明它的if块内。 如果我们将对象声明为静态,现在让我们看看输出的变化。

#include using namespace std; class Apple { int i; public: Apple() { i = 0; cout << "Inside Constructor\n"; } ~Apple() { cout << "Inside Destructor\n"; } }; int main() { int x = 0; if (x==0) { static Apple obj; } cout << "End of main\n"; } //输出 Inside Constructor End of main Inside Destructor

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

可以清楚地看到输出的变化。现在,在main结束后调用析构函数。这是因为静态对象的范围是贯穿程序的生命周期。

2.2、类中的静态函数

就像类中的静态数据成员或静态变量一样,静态成员函数也不依赖于类的对象。我们被允许使用对象和.来调用静态成员函数。但建议使用类名和范围解析运算符调用静态成员。

允许静态成员函数仅访问静态数据成员或其他静态成员函数,它们无法访问类的非静态数据成员或成员函数。

#include using namespace std; class Apple { public: // static member function static void printMsg() { cout<<"Welcome to Apple!"; } }; // main function int main() { // invoking a static member function Apple::printMsg(); } //输出 Welcome to Apple!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

C++

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:识别全局,快人一步丨华为云态势感知上新!
下一篇:设计模式之——单例模式
相关文章