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

  

Static data members in C++的更多相关文章

  1. Static Classes and Static Class Members

    Static Classes and Static Class Members A static class is basically the same as a non-static class, ...

  2. Link static data in sql source control

    You can link data that doesn't change very often to SQL Source Control. This lets you commit data ch ...

  3. c# Use Properties Instead of Accessible Data Members

    advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...

  4. Simplest way to serve static data from outside the application server in a Java web application

    tomcat service.xml <Context docBase="/path/to/images" path="/images" /> re ...

  5. [EffectiveC++]item22:Declare data members private

    将成员变量隐藏在函数接口的背后,可以为“所有可能的实现”提供弹性, 假设我们有一个public成员变量,而我们最终取消了它,多少代码可能会被破坏呢?那是一个不可知的大量. protected成员变量就 ...

  6. 条款22:将成员变量声明为private(Declare data members private)

    NOTE: 1.切记将成员变量声明为private.这可赋予客户访问数据的一致性 可细微划分访问控制 允诺约束条件获得保证,并提供class作者以充分的实现弹性. 2.protected 并不比pub ...

  7. Initialization of data members

    In C++, class variables are initialized in the same order as they appear in the class declaration. C ...

  8. C++ essentials 之 static 关键字

    extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...

  9. Data 语义学(1)

    一.Data Member 的绑定(The binding of Data Member) extern float x; class Point3d { public: Point3d( float ...

随机推荐

  1. Pip安装Django超时(time out)解决方法

    (ll_env)learning_log$  pip install Django  执行该命令,始终报错,如上图 解决方法如下: pip install  -i http://pypi.douban ...

  2. mac下将python2.7改为python3

    mac下将python2.7改为python3 查看当前电脑python版本 python -V 修改.bash_profile文件 vi ~/.bash_profile //编辑bash_profi ...

  3. Linux系统僵尸进程详解

    大安好,我是良许. 本文我们将来讨论一下什么是僵尸进程,僵尸进程是怎么产生的,如何杀死一个僵尸进程. Linux中的进程是什么? 讲到进程,我们要先了解一下另一个概念:程序. 程序说白了就是躺在电脑硬 ...

  4. 设计模式学习-使用go实现外观模式

    外观模式 定义 适用范围 代码实现 优点 缺点 关于接口粒度的思考 参考 外观模式 定义 外观模式也叫门面模式 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接 ...

  5. python读写文件with open

    简介 使用python的过程中肯定少不了读取文件的操作, 传统的形式是使用 直接打开.然后在操作.然后再关闭, 这样代码量稍微大些不说,一旦在操作步骤中出现报错,则无法进行文件的关闭: 案例一(读取) ...

  6. C语言通过指针数组和二维数组读取文件

    1 # include <stdio.h> 2 # include <stdlib.h> 3 # include <time.h> 4 # include < ...

  7. [loj6736]最小连通块

    定义$f(S)$表示点集$S$的最小连通块 做法1 通过对所有节点判定,可以在$n$次询问中求出具体的$f(S)$ 对于$x\ne y$,显然$(x,y)\in E$当且仅当$f(\{x,y\})=\ ...

  8. [cf1392H]ZS Shuffles Cards

    考虑统计每一轮(以抽到小丑为一轮)的贡献,不难发现答案即期望轮数*每轮期望次数 关于期望轮数,当前牌堆里已经在$S$中的卡实际上没有意义,不妨将这一类卡从牌堆中删除 此时,定义$f_{i}$表示$S$ ...

  9. [bzoj1280]卖猪

    首先考虑猪无法流动,那么源点向每一个猪圈连猪圈中猪个数的边,每一个顾客向汇点连所需猪的边,每一个猪圈向能打开它的顾客连inf的边,跑最大流即可. 但考虑猪要流动,有一个十分巧妙地做法,将每一个顾客所有 ...

  10. 从零开始,使用Dapr简化微服务

    序言 现有的微服务模式需要再业务代码中集成大量基础设施模块,比如注册中心,服务发现,服务调用链路追踪,请求熔断,重试限流等等,使得系统过于臃肿重量级. Dapr作为新一代微服务模式,使用sidecar ...