赋值运算符和初始化 [英] The assignment operator and initialization

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

问题描述

我在学习C ++编程语言,我正在阅读关于赋值运算符(=)的章节。在C ++初始化和赋值操作是类似的,我们可以使用相同的符号。

I'm studying the C++ programming language and I'm reading the chapter about assignment operator ( = ). In C++ initalization and assignment are operation so similar that we can use the same notation.

但我的问题是:当我初始化一个变量, ?当我分配一个变量,我用赋值运算符吗?我认为唯一的区别是初始化和赋值之间,因为当我们初始化一个变量,我们给它一个新的值与assignmnet运算符,当我们分配到一个变量,我们用一个新的值替换旧的值使用赋值运算符。这样对吗 ?

But my question is : when i initialize a variable am I doing it with the assignment operator ? When i assign to a variable, am I doing it with the assignment operator ? I think that the only difference is between initialization and assignment because when we initalize a variable we are giving it a new value with the assignmnet operator, when we assign to a variable we are replacing the old value of that variable with a new value using the assignment operator. Is it right ?

推荐答案

当您要求


我用赋值运算符初始化一个变量?

when i initialize a variable am I doing it with the assignment operator ?


当我们初始化一个变量时,我们用赋值运算符给它一个新的值

when we initialize a variable we are giving it a new value with the assignment operator

, 不,你不是。符号 = 用于复制初始化和赋值,但初始化不使用赋值运算符。一个变量的初始化实际上使用一个构造函数。

But, no, you are not. The symbol = is used for both copy-initialization and assignment, but initialization does NOT use the assignment operator. Initialization of a variable actually uses a constructor.

在复制初始化中,它使用一个复制构造函数。

In copy-initialization, it uses a copy constructor.

type x = e; // NOT an assignment operator

第一 e 被转换为类型,创建一个临时变量,然后 type :: type(const type&) initializes x type :: operator =(const type&)根本不被调用。

First e is converted to type, creating a temporary variable, and then type::type(const type&) initializes x by copying that temporary. type::operator=(const type&) is not called at all.

不使用 = 符号:

type x(e);
type x{e}; // since C++11
otherclass::otherclass() : x(e) {} // initialization of member variable

虽然初始化和赋值给一个变量一个值,两者不使用相同的代码。

While both initialization and assignment give a variable a value, the two do not use the same code to do it.

更多详细信息:使用C ++ 11和更高版本,如果有一个移动构造函数,复制初始化将使用它,因为转换的结果是一个临时的。此外,在复制初始化中,编译器允许跳过实际调用复制或移动构造函数,它可以将初始化程序直接转换为变量。但它仍然必须检查副本或移动构造函数是否存在并且可访问。而复制构造函数也可以采用非const引用。所以它可能是 type :: type(type&&&),或 type :: type(const type&& / code>(非常不常见),或使用 type :: type(type&),而不是 type :: type const type&)。我上面描述的是最常见的情况。

Further details: With C++11 and later, if there is a move constructor, copy initialization will use it instead, because the result of the conversion is a temporary. Also, in copy-initialization, the compiler is permitted to skip actually calling the copy or move constructor, it can convert the initializer directly into the variable. But it still must check that the copy or move constructor exists and is accessible. And copy constructors can also take a non-const reference. So it might be type::type(type&&) that is used, or type::type(const type&&) (very uncommon), or type::type(type&) that is used, instead of type::type(const type&). What I described above is the most common case.

这篇关于赋值运算符和初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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