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
Static data members in C++的更多相关文章
- Static Classes and Static Class Members
Static Classes and Static Class Members A static class is basically the same as a non-static class, ...
- 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 ...
- 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 ...
- 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 ...
- [EffectiveC++]item22:Declare data members private
将成员变量隐藏在函数接口的背后,可以为“所有可能的实现”提供弹性, 假设我们有一个public成员变量,而我们最终取消了它,多少代码可能会被破坏呢?那是一个不可知的大量. protected成员变量就 ...
- 条款22:将成员变量声明为private(Declare data members private)
NOTE: 1.切记将成员变量声明为private.这可赋予客户访问数据的一致性 可细微划分访问控制 允诺约束条件获得保证,并提供class作者以充分的实现弹性. 2.protected 并不比pub ...
- Initialization of data members
In C++, class variables are initialized in the same order as they appear in the class declaration. C ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- Data 语义学(1)
一.Data Member 的绑定(The binding of Data Member) extern float x; class Point3d { public: Point3d( float ...
随机推荐
- MapReduce Service更换集群外部时钟源,仅需10步
摘要:MapReduce Service 集群使用NTP进行时钟同步.本文简要介绍了MapReduce Service集群NTP机制及NTP的配置方式. 本文分享自华为云社区<MapReduce ...
- 【GIS】GeoServer服务Authkey配置记录
特别感谢:https://www.cnblogs.com/HandyLi/p/8624507.html 1.服务受控配置 2.授权方式 3.Url模式配置 4.Authkey密钥配置 5.使用 在wm ...
- Python科普系列——类与方法(上篇)
欢迎来到新的系列,up又开新坑了~~ 实际上,Python作为一门易用性见长的语言,看上去简单,却仍然有很多值得一说的内容,因此这个系列会把Python中比较有意思的地方也给科普一遍.而另一方面,关于 ...
- 菜鸡的Java笔记 简单JAVA 类的开发原则以及具体实现
/* 现在要求定义一个雇员信息类 在这个类之中包含有雇员编号 姓名 职位 基本工资 佣金等信息 对于此时给定要求实际上就是描述一类事物,而这样的程序类在在java之中可以将其称为简单java类 ...
- halcon语法讲解
前言 最近换工作,在学习了解halcon工具,每天总结分析今天所学知识,今天是基础语法篇! 1.基本语法 描述 语法 等号 := 不等号 # 注释符 * 字符串赋值 str:='halcon' 等于比 ...
- [cf1184E]Daleks' Invasion
先求出任意一棵最小生成树,然后对边分类讨论1.非树边,答案即最小生成树的环上的最长边2.树边,反过来考虑,相当于对于每一个点对那条路经打上标记,取min对于1直接用倍增维护即可,对于2可以用树链剖分/ ...
- HCNP Routing&Switching之组播技术-组播基础
组播技术背景 随着internet网络的不断发展,网络中交互的各种数据.语音.视频信息数量突增:新型的在线直播.网络电视.视频会议等应用也在逐渐兴起:这些业务大多符合点到多点的模式,对信息安全性.传播 ...
- 【JavaSE】JDK配置
Java开发环境配置 2020-09-10 08:32:20 by冲冲 1. Windows7安装JDK 1.1 下载JDK ① 下载地址:http://www.oracle.com/techne ...
- Hi3516开发笔记(四):Hi3516虚拟机编译uboot、kernel、roofts和userdata以及分区表
若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/121572767红胖子(红模仿)的博文大全:开发技术集合( ...
- Codeforces 1288F - Red-Blue Graph(上下界网络流)
Codeforces 题面传送门 & 洛谷题面传送门 好久没有写过上下界网络流了,先来一题再说( 首先先假设所有边都是蓝边,那么这样首先就有 \(b\times m\) 的花费,但是这样不一定 ...