copy initialization
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
初始化一个对象从另一个对象
Original:
Initializes an object from another object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 语法
T object = other ;
|
(1) | ||||||||
f(other);
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
[编辑] 解释
复制初始化在下列情况下进行
Original:
Copy initialization is performed in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
当一个命名的变量(自动,静态的,或本地线程)的声明的初始值由一个等号后跟表达式.
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
当传递一个参数给函数的值
Original:
when passing an argument to a function by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3)
返回时,从一个函数返回的值
Original:
when returning from a function that returns by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
4)
当捕获异常的价值
Original:
when catching an exception by value
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
5)
集合初始化的一部分,初始化每个元素的初始值设定项
Original:
as part of 集合初始化, to initialize each element for which an initializer is provided
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
拷贝初始化的效果是:1
Original:
The effects of copy initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- 如果
T是一个类类型,该类型的other的简历不合格版本的T或一类来自T,T的构造研究和选择最佳匹配的重载决议。然后调用该构造函数初始化对象.Original:IfTis a class type and the type of other is cv-unqualified version ofTor a class derived fromT, the constructors ofTare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 如果
T是一个类类型,和类型的other是不同的,或如果T非类的类型,但类型other是一个类的类型,它可以转换从类型用户定义的转换序列otherT检查和最佳的一种是通过重载决议。转换的结果,这是一个临时目标类型的prvalue,然后使用直接初始化对象。最后一步是淘汰和结果的转换功能直接构建在目标对象的内存分配,但需要拷贝构造函数可以访问,即使它不使用.Original:IfTis a class type, and the type of other is different, or ifTis non-class type, but the type of other is a class type, 用户定义的转换序列 that can convert from the type of other toTare 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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- 否则(如果没有
T也不other的类型是类类型),标准的转换中使用,如果必要的话,other的cv不合格版本的T的值转换.Original:Otherwise (if neitherTnor the type of other are class types), 标准的转换 are used, if necessary, to convert the value of other to the cv-unqualified version ofT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 注释
复制比直接初始化:复制初始化只考虑非显式构造函数和用户定义的转换函数的初始化许可.
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
other是一个右值表达式,移动的构造函数会选择重载决议,并呼吁在拷贝初始化.
Original:
If other is an rvalue expression, 移动的构造函数 will be selected by overload resolution and called during copy-initialization.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
等号,
=,复制初始化命名的变量是不相关的赋值运算符。复制初始化赋值运算符重载有没有影响.Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#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 }

