惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
博客园_首页
量子位
博客园 - 司徒正美
S
SegmentFault 最新的问题
J
Java Code Geeks
小众软件
小众软件
博客园 - 【当耐特】
The Register - Security
The Register - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
Cyberwarzone
Cyberwarzone
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
V
Vulnerabilities – Threatpost
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
F
Full Disclosure
D
DataBreaches.Net
Martin Fowler
Martin Fowler
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 最新话题
云风的 BLOG
云风的 BLOG
C
Check Point Blog
T
Threatpost
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
W
WeLiveSecurity
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
Last Week in AI
Last Week in AI
T
Tor Project blog
T
Troy Hunt's Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security Affairs
SecWiki News
SecWiki News

博客园 - 中土

strcpy和memcpy的标准实现 Linux 命令缩写部分解释 Eclipse RCP 的一些有用的资源及应用案例 分析Linux和windows动态库 C/C++中的函数参数传递机制(zz) LDD3 笔记: 第3章 字符设备的驱动 2008年看书计划 H.264学习建议(zz) - 中土 - 博客园 几种常用的视频接口(端子) U-boot 移植到FS2410 NandFlash和NorFlash的区别 A simple tourial for Linux 2.6.24 kernel module Linux basic use Kernel resource list Linux2.6 驱动设计―从 2.4.x 到 2.6.x Linux 2.6.x 内核模块入门(LKM) C++中无处不在的临时变量 C++初始化与赋值 C异常处理实现: setjmp和longjmp
类中引用成员的初始化
中土 · 2008-10-08 · via 博客园 - 中土

1. A hard-to-find problem

See the following code snippet:

class Test
{
public:
    Test(int val) : ref_(val){}
    ~Test() {}
private:
    int & ref_;
};
 
int main()
{
    Test t(1);
 
    return 0;
}

what problem do you see? Maybe nothing.

Yeah, you initialize the Test::ref_ by a value of type "int", that's ref_(val)

but if you try directly:

int & ref1 = 2; // complier complain!

why?

Actually, the Test::ref_ is initialized in the constructor, a FUNCTION. That's mean ref_ is initialized by an automaic variable(formal argument val), not just a literation constant like 2. So that's true at 1st snippet.

But you can't initialize a non-const reference directly by a literation constant in 2nd snippet. Instead, you can do:

Here, a temporary with type of const int have been generated, and then this temporary can initialize the const int & reference.

Specially, in the following case, an extra type conversion has occured before the initialization:

2. Problem again and improvement

In 1st snippet, the Test::ref_ actually refers to an automatic variable, which have been destroyed after constructor invocation. So the approach for initializing the member of reference type is terriblely dangerous!

A possible approach is like this:

class Test
{
public:
    Test(int &val) : ref_(val){}
    ~Test() {}
private:
    int & ref_;
};
 
int main()
{
    Test t(1);
 
    return 0;
}

3. Why must use member initialization list for reference data member and const data member?

First, you should understand the advantage of member initialization list.

In 3 cases, you must use member initialization list:

1) Object member(no built-in type)

2) const member

3) reference member

Because const and reference type must intialize when declaration(defination), you cannot just declare without initialization them at first, and then initialize them by assignment.

So it's very important to differentiate the distinction between initialization and assignment. At fact, the complier can strictly disguish the difference!

You can try:

class Test
{
public:
    Test(char ch, int &val) { ch_=ch; ref_=val; }
    ~Test() {}
private:
    const char ch_;
    int & ref_;
};
 
int main()
{
    int a=2;
    Test t('c', a);
 
    return 0;
}
 

This time, the complier will complain some warnings.. ah

------
assignment of read-only data-member `Test::ch_'    main.cc    cpp    4    C/C++ Problem
uninitialized member `Test::ch_' with `const' type `const char'    main.cc    cpp    4    C/C++ Problem
uninitialized reference member `Test::ref_'    main.cc    cpp    4    C/C++ Problem

Summary:

1) All the operations in the body of contructor are regarded as assignment but not initialization.

2) reference and const type MUST be initialized when and only when they are declared(defined, if exactly speaking).