直接初始化和复制引用的初始化 [英] direct initialization and copy initialization of reference

查看:116
本文介绍了直接初始化和复制引用的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读C ++引物。对于对象的初始化,C ++支持两种形式的初始化:直接和复制。
但是书中没有引用初始化的引用。在书中我从来没有见过直接初始化(如果存在)的引用。所有是复制一个像:

I have been reading "C++ primer". For the initialization of object, C++ supports 2 forms of initialization: direct and copy. but the book does not refer the initialization of reference. And in the book I have never seen the direct initialize(if exists) of a reference. All is the copy one like:

int i;
int &j = i;//but not int &j(i);which also works in my experiment

我想知道的是,它是相同的下面为初始化一个引用。
代表以下代码:

I want to know that is it the same that are going on underneath for the initialization of a reference. for the following codes:

string null_book = "9-999-99999-9";

初始化进度首先创建一个临时字符串对象tmp(例如),将直接初始化为ac样式字符串参数,然后使用副本构造函数初始化变量null_book。这对我有意义。
对于这一个:

the initialization progress is first create a temporary string object tmp(for instance) that will direct initialized with a c style string parameter, and then initialize the variable null_book with the copy Constructor. That make sense to me. for this one:

int &j = i;

将以同样的方式初始化ref j?这将是一个由i初始化的温度参考(例如),然后用t初始化j。这不是有意义吗?为什么书从不使用直接初始化来参考?
感谢您的关注!

will ref j be initialized the same way? That will be a temp reference it &t(for example) initialized by i and then initialize j with t? that doesnt make sense??? Why the book never use the direct initialization for reference? Thanks for your attention!

推荐答案

直接初始化和复制初始化的主要区别在8.5节,标准第17段。一般来说,区别在于对于复制初始化中的类类型,不考虑显式构造函数(仅考虑转换构造函数),并创建可能的复制副本;在直接初始化显式构造函数中,并且直接构造目标。从标准的第8.5节:

The main differences between direct-initialization and copy-initialization are covered in section 8.5, paragraph 17 of the standard. In general, the difference is that for class types in copy-initialization, explicit constructors are not considered (only converting constructors are considered) and a possibly elided copy is made; in direct-initialization explicit constructors are considered and the target is constructed directly. From section 8.5 of the standard:


14 - 初始化的形式(使用括号或 = )通常是无关紧要的,但是当初始化器或被初始化的实体有类类型[...]时非常重要。

14 - The form of initialization (using parentheses or =) is generally insignificant, but does matter when the initializer or the entity being initialized has a class type [...]

对于非类类型(包括引用),直接初始化和复制初始化具有类似的语义;对于引用,引用绑定在两种情况下都会发生,如 8.5.3引用[dcl.init.ref] 中所指定。引用的直接初始化和复制初始化只有涉及转换函数的不同语义( 13.3.1.6通过直接引用绑定的转换函数初始化[over.match.ref] );

For non-class types (including references), direct-initialization and copy-initialization have similar semantics; for references, a reference binding occurs in either case, as specified in 8.5.3 References [dcl.init.ref]. Direct-initialization and copy-initialization of a reference only have different semantics where a conversion function is involved (13.3.1.6 Initialization by conversion function for direct reference binding [over.match.ref]); again, direct-initialization is allowed to invoke explicit conversion functions where copy-initialization is not.

因此,在

int &j = i;

8.5.3p5适用,参考 j 直接绑定到 i

8.5.3p5 applies and the reference j is bound directly to the lvalue i. No temporaries are invoked.

在复杂性方面,引用更接近基本类型(原始类型)而不是类类型。基元被初始化而没有临时构造(8.5p17,最后一个项目符号),一般来说引用也是。这可能是为什么本书只使用 = 形式来初始化引用;与原语一样,通常没有区别,并且 int i = x; 通常比 int i(x);

In terms of complexity, references are closer to fundamental (primitive) types than to class types. Primitives are initialized without a temporary being constructed (8.5p17, last bullet) and in general references are too. This is probably why the book only uses the = form for initialization of references; as with primitives, there is usually no difference and writing int i = x; is usually clearer than int i(x);.

这篇关于直接初始化和复制引用的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆