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

推荐订阅源

Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
The Register - Security
The Register - Security
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园_首页
U
Unit 42
T
Tailwind CSS Blog
G
GRAHAM CLULEY
F
Full Disclosure
V
Vulnerabilities – Threatpost
T
Tenable Blog
月光博客
月光博客
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
K
Kaspersky official blog
Scott Helme
Scott Helme
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LINUX DO - 最新话题
Recorded Future
Recorded Future
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
The GitHub Blog
The GitHub Blog
Cisco Talos Blog
Cisco Talos Blog
SecWiki News
SecWiki News
P
Proofpoint News Feed
Security Latest
Security Latest
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
罗磊的独立博客
S
Security Affairs
M
MIT News - Artificial intelligence
L
LINUX DO - 热门话题
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
V
Visual Studio Blog

博客园 - 中土

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).