C++静态成员和非静态成员的区别和使用

  •   对象与对象之间的成员变量是相互独立的。要想共用数据,则需要使用静态成员和静态方法。
  •   只要在类中声明静态成员变量,即使不定义对象,也可以为静态成员变量分配空间,进而可以使用静态成员变量。(因为静态成员变量在对象创建之前就已经被分配了内存空间)
  •   静态成员变量虽然在类中,但它并不是随对象的建立而分配空间的,也不是随对象的撤销而释放(一般的成员在对象建立时会分配空间,在对象撤销时会释放)。静态成员变量是在程序编译时分配空间,而在程序结束时释放空间。
  •   静态成员的定义和声明要加个关键static。静态成员可以通过双冒号来使用,即<类名>::<静态成员名>。
  •   初始化静态成员变量要在类的外面进行。初始化的格式如下:数据类型  类名::静态成员变量名 = 初值;
  •   不能用参数初始化表,对静态成员变量进行初始化。
  •   既可以通过类名来对静态成员变量进行引用,也可以通过对象名来对静态成员变量进行引用。
  •   普通成员函数和静态成员函数的区别是:普通成员函数在参数传递时编译器会隐藏地传递一个this指针.通过this指针来确定调用类产生的哪个对象;但是静态成员函数没有this指针,不知道应该访问哪个对象中的数据,所以在程序中不可以用静态成员函数访问类中的普通变量.

  下面通过几个例子来总结静态成员变量和静态成员函数的使用规则。

  一、通过类名调用静态成员函数和非静态成员函数

 1 //例子一:通过类名调用静态成员函数和非静态成员函数
2 class Point{
3 public:
4 void init()
5 {}
6
7 static void output()
8 {}
9 };
10
11 void main()
12 {
13 Point::init();//此处编译错误 非静态成员函数必须和对象对应
14 Point::output();
15 }

  编译出错:错误 1 error C2352: “Point::init”: 非静态成员函数的非法调用

  结论一:不能通过类名来调用类的非静态成员函数

  二、通过类的对象调用静态成员函数和非静态成员函数

 1 //例子二:通过类的对象调用静态成员函数和非静态成员函数
2 class Point{
3 public:
4 void init()
5 {
6 }
7
8 static void output()
9 {}
10 };
11
12 void main()
13 {
14 Point pt;
15 pt.init();
16 pt.output();
17 }

  编译通过。

  结论二:类的对象可以使用静态成员函数和非静态成员函数。

  三、在类的静态成员函数中使用类的非静态成员

 1 //例子三:在类的静态成员函数中使用类的非静态成员
2 #include <iostream>
3 using namespace std;
4
5 class Point{
6 public:
7 void init()
8 {
9 }
10 static void output()
11 {
12 cout << "m_x=" << m_x << endl;//此处编译错误 静态成员函数中不能引用非静态成员
13 }
14 private:
15 int m_x;
16 };
17 void main()
18 {
19 Point pt;
20 pt.output();
21 }

  编译出错:IntelliSense: 非静态成员引用必须与特定对象相对

  因为静态成员函数属于整个类,在类实例化对象之前就已经分配空间了,而类的非静态成员必须在类实例化对象后才有内存空间,所以这个调用就会出错,就好比没有声明一个变量却提前使用它一样。

  结论三:静态成员函数中不能引用非静态成员。

  四、在类的非静态成员函数中使用类的静态成员

 1 //例子四:在类的非静态成员函数中使用类的静态成员
2 #include <iostream>
3 using namespace std;
4
5 class Point{
6 public:
7 void init()
8 {
9 output();
10 }
11 static void output()
12 {
13 }
14 private:
15 int m_x;
16 };
17 void main()
18 {
19 Point pt;
20 pt.init();
21 }

  编译通过。

  结论四:类的非静态成员可以调用静态成员函数,但反之不能。

  五、使用类的静态成员变量

 1 //例子五:使用类的静态成员变量
2 #include <iostream>
3 using namespace std;
4
5 class Point{
6 public:
7 Point()
8 {
9 m_nPointCount++;
10 }
11 ~Point()
12 {
13 m_nPointCount++;
14 }
15 static void output()
16 {
17 cout << "m_nPointCount=" << m_nPointCount << endl;
18 }
19 private:
20 static int m_nPointCount;
21 };
22
23 void main()
24 {
25 Point pt;
26 pt.output();
27 }

  

  链接出错:error LNK2001: 无法解析的外部符号 "private: static int Point::m_nPointCount" (?m_nPointCount@Point@@0HA)

  这是因为类的成员变量在使用前必须先初始化。

  改成如下代码即可:

 1 #include <iostream>
2 using namespace std;
3
4 class Point{
5 public:
6 Point()
7 {
8 m_nPointCount++;
9 }
10 ~Point()
11 {
12 m_nPointCount++;
13 }
14 static void output()
15 {
16 cout << "m_nPointCount=" << m_nPointCount << endl;
17 }
18 private:
19 static int m_nPointCount;
20 };
21
22 //类外初始化静态成员变量时,不用带static关键字
23 int Point::m_nPointCount = 0;
24 void main()
25 {
26 Point pt;
27 pt.output();
28 }

  运行结果:

  

  结论五:类的静态成员变量必须先初始化再使用。

  

最新文章

  1. Oracle监听服务启动失败案例
  2. EJB的调用
  3. [并查集] POJ 1182 食物链
  4. Spark External Datasets
  5. WebApi系列~基于单请求封装多请求的设计~请求的安全性设计与实现
  6. java反射的基础学习代码
  7. hdu 4336 Card Collector
  8. 设计模式之美:Builder(生成器)
  9. FR报表 自动缩小的代码
  10. Retina屏下1px border
  11. Android textview 设置不同的字体大小和颜色
  12. 201521123023《Java程序设计》第11周学习总结
  13. C#安装Telnet
  14. SQL Server获取星期几
  15. 《JAVA编程思想》第四版 PDF
  16. Learn Python3 the hard way 第二天总结 命令行(2)
  17. 关于vivado implement后clock interaction报告的理解(更新中)
  18. Ubuntu局域网下利用client联网
  19. LeetCode 35 Search Insert Position(查找插入位置)
  20. [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design

热门文章

  1. IDEA debug
  2. 关于LOH(Large Object Heap)及内存泄漏
  3. Using Mono DLLs in a Unity Project
  4. ArcGIS js api三种查询功能
  5. [Plan]计划
  6. C#根据URL生成签名
  7. maven filter插件只替换了部分变量问题
  8. python 全栈基础作业题
  9. code1064 虫食算
  10. C# 基础连接已经关闭: 发送时发生错误