zoukankan      html  css  js  c++  java
  • Static data members in C++

     

      Predict the output of following C++ program:

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class A
     5 {
     6 public:
     7     A() { cout << "A's Constructor Called " << endl;  }
     8 };
     9  
    10 class B
    11 {
    12     static A a;
    13 public:
    14     B() { cout << "B's Constructor Called " << endl; }
    15 };
    16  
    17 int main()
    18 {
    19     B b;
    20     return 0;
    21 }

      Output:

      B's Constructor Called
      

      The above program calls only B’s constructor, it doesn’t call A’s constructor.

      The reason for this is simple, static members are only declared in class declaration, not defined. They must be explicitly defined outside the class using scope resolution operator.
      

      If we try to access static member ‘a’ without explicit definition of it, we will get compilation error. For example, following program fails in compilation.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class A
     5 {
     6     int x;
     7 public:
     8     A() 
     9     { 
    10         cout << "A's constructor called " << endl;  
    11     }
    12 };
    13  
    14 class B
    15 {
    16     static A a;
    17 public:
    18     B() 
    19     { 
    20         cout << "B's constructor called " << endl; 
    21     }
    22     static A getA() 
    23     { 
    24         return a; 
    25     }
    26 };
    27  
    28 int main()
    29 {
    30     B b;
    31     A a = b.getA();
    32     return 0;
    33 }

      Output:

      Compiler Error: undefined reference to `B::a'
      

      If we add definition of a, the program will works fine and will call A’s constructor. See the following program.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class A
     5 {
     6     int x;
     7 public:
     8     A() 
     9     { 
    10         cout << "A's constructor called " << endl;  
    11     }
    12 };
    13  
    14 class B
    15 {
    16     static A a;
    17 public:
    18     B() 
    19     { 
    20         cout << "B's constructor called " << endl; 
    21     }
    22     static A getA() 
    23     { 
    24         return a; 
    25     }
    26 };
    27  
    28 A B::a;  // definition of a
    29  
    30 int main()
    31 {
    32     B b1, b2, b3;
    33 
    34     A a = b1.getA();
    35  
    36     return 0;
    37 }

      Output:

      A's constructor called
      B's constructor called
      B's constructor called
      B's constructor called
      

      Note that the above program calls B’s constructor 3 times for 3 objects (b1, b2 and b3), but calls A’s constructor only once. The reason is, static members are shared among all objects. That is why they are also known as class members or class fields. Also, static members can be accessed without any object.

      See the below program where static member ‘a’ is accessed without any object.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class A
     5 {
     6     int x;
     7 public:
     8     A() 
     9     { 
    10         cout << "A's constructor called " << endl;  
    11     }
    12 };
    13  
    14 class B
    15 {
    16     static A a;
    17 public:
    18     B() 
    19     { 
    20         cout << "B's constructor called " << endl; 
    21     }
    22     static A getA() 
    23     { 
    24         return a; 
    25     }
    26 };
    27  
    28 A B::a;  // definition of a
    29  
    30 int main()
    31 {
    32     // static member 'a' is accessed without any object of B
    33     A a = B::getA();
    34  
    35     return 0;
    36 }

      Output:

      A's constructor called

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
        

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-26  09:05:13

      

  • 相关阅读:
    vs中添加wsdl生成代理类工具
    vscode+prettier 设置保存自动格式化
    k8s 部署项目
    Jmate使用
    k8s部署项目
    docker 打包镜像 部署项目
    vs2012编译xp运行的mfc程序InitializeCriticalSectionEx解决方案
    thinkphp 入口文件 iis 500错误
    java初学之stream
    php preg_match正则长度限制
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442629.html
Copyright © 2011-2022 走看看