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

推荐订阅源

Jina AI
Jina AI
MyScale Blog
MyScale Blog
量子位
月光博客
月光博客
J
Java Code Geeks
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
U
Unit 42
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
G
Google Developers Blog
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
IT之家
IT之家
N
Netflix TechBlog - Medium
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
B
Blog
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
B
Blog RSS Feed

博客园 - 中土

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