C++ 类的静态成员及静态成员函数

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

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

  1、经过类名调用静态成员函数和非静态成员函数函数

复制代码

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”: 非静态成员函数的非法调用this

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

 

  2、经过类的对象调用静态成员函数和非静态成员函数.net

复制代码

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 }

复制代码

  编译经过。指针

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

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

复制代码

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: 非静态成员引用必须与特定对象相对内存

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

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

 

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

复制代码

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 }

复制代码

  编译经过。

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

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

复制代码

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 }

复制代码

  运行结果:

  

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

相关文章
相关标签/搜索