copy initialization
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
初始化一个对象从另一个对象
原文:
Initializes an object from another object
目录 |
[编辑] 语法
T object = other ;
|
(1) | ||||||||
f( other) ;
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
[编辑] 解释
复制初始化在下列情况下进行
原文:
Copy initialization is performed in the following situations:
1)
当一个命名的变量(自动,静态的,或本地线程)的声明的初始值由一个等号后跟表达式.
原文:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
2)
当传递一个参数给函数的值
原文:
when passing an argument to a function by value
3)
返回时,从一个函数返回的值
原文:
when returning from a function that returns by value
4)
当捕获异常的价值
5)
拷贝初始化的效果是:1
原文:
The effects of copy initialization are:
- 如果
T
是一个类类型,该类型的other的简历不合格版本的T
或一类来自T
,T
的构造研究和选择最佳匹配的重载决议。然后调用该构造函数初始化对象.原文:IfT
is a class type and the type of other is cv-unqualified version ofT
or a class derived fromT
, the constructors ofT
are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
- 如果
T
是一个类类型,和类型的other是不同的,或如果T
非类的类型,但类型other是一个类的类型,它可以转换从类型用户定义的转换序列otherT
检查和最佳的一种是通过重载决议。转换的结果,这是一个临时目标类型的prvalue,然后使用直接初始化对象。最后一步是淘汰和结果的转换功能直接构建在目标对象的内存分配,但需要拷贝构造函数可以访问,即使它不使用.原文:IfT
is a class type, and the type of other is different, or ifT
is non-class type, but the type of other is a class type, 用户定义的转换序列 that can convert from the type of other toT
are examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to 直接初始化 the object. The last step is usually 淘汰 and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.
[编辑] 注释
复制比直接初始化:复制初始化只考虑非显式构造函数和用户定义的转换函数的初始化许可.
原文:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
other是一个右值表达式,移动的构造函数会选择重载决议,并呼吁在拷贝初始化.
原文:
If other is an rvalue expression, 移动的构造函数 will be selected by overload resolution and called during copy-initialization.
等号,
=
,复制初始化命名的变量是不相关的赋值运算符。复制初始化赋值运算符重载有没有影响.原文:
The equals sign,
=
, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.[编辑] 示例
#include <string> #include <utility> #include <memory> int main() { std::string s = "test"; // OK: constructor is non-explicit std::string s2 = std::move(s); // this copy-initialization performs a move // std::unique_ptr<int> p = new int(1); // error: constructor is explicit std::unique_ptr<int> p(new int(1)); // OK: direct-initialization int n = 3.14; // floating-integral conversion const int b = n; // const doesn't matter int c = b; // ...either way }