zoukankan      html  css  js  c++  java
  • C++静态成员和非静态成员的区别和使用

     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 }
    复制代码

      运行结果:

      

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

      

  • 相关阅读:
    HDU1266 Reverse Number
    codevs1380 没有上司的舞会
    codevs1163 访问艺术馆
    codevs2144 砝码称重 2
    codevs1553 互斥的数
    codevs1230 元素查找
    codevs3118 高精度练习之除法
    codevs1245 最小的N个和
    codevs1063 合并果子
    codevs1052 地鼠游戏
  • 原文地址:https://www.cnblogs.com/tsh292278/p/10471189.html
Copyright © 2011-2022 走看看