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

  

最新文章

  1. 应用新安全组 - 每天5分钟玩转 OpenStack(116)
  2. docker学习(5) 在mac中创建mysql docker容器
  3. ASP.NET MVC 路由(三)
  4. 在Linux操作系统下备份恢复技术的应用 转自https://yq.aliyun.com/articles/50205?spm=5176.100239.blogcont24250.9.CfBYE9
  5. vim 替换操作
  6. phonegap 单例模式
  7. Windows Azure 虚拟网络配置(Point to Site)
  8. linux常用命令之--文本编辑和文本内容查看命令
  9. IIS (HTTP Error 500.21 - Internal Server Error)解决
  10. eval 如何定义函数
  11. OpenCV初探
  12. 使用Go和Let&#39;s Encrypt证书部署HTTPS
  13. 201521123033《Java程序设计》第1周学习总结
  14. WEB项目的部署结构
  15. Django高级部分
  16. temp--内蒙农信(环境)
  17. 搭建ELK集群
  18. [转]仿91助手的PC与android手机通讯
  19. C#中的特性 (Attribute) 入门 (一)
  20. Design Pattern in Simple Examples

热门文章

  1. centos yum更换阿里镜像
  2. uni-app nvue页面动态修改导航栏按钮
  3. Merge into用法总结
  4. Emmet快速语法—助力HTML/CSS一行代码一个页面
  5. asp.net.core教程
  6. 从源码分析 XtraBackup 的备份原理
  7. 微信小程序(八)
  8. 日记啦QWWQ
  9. [nowcoder5667H]Happy Triangle
  10. 规格模式(Specification Pattern)