转:copy initialization

转自: http://en.cppreference.com/w/cpp/language/copy_initialization

copy initialization

Initializes an object from another object

Syntax

 
T object other ;(1) 
 
f(other);(2) 
 
return other;(3) 
 
catch ( T other) ;(4) 
 
T array [ N ] = { other };(5) 
 

Explanation

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) when catching an exception by value
5) as part of  aggregate initialization, to initialize each element for which an initializer is provided

The effects of copy initialization are:

  • If T is a class type and the type of other is cv-unqualified version of T or a class derived from T, the constructors of Tare examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
  • If T is a class type, and the type of other is different, or if T is non-class type, but the type of other is a class type,user-defined conversion sequences that can convert from the type of other to T 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 direct-initialize the object. The last step is usually eliminated 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.
  • Otherwise (if neither T nor the type of other are class types), standard conversions are used, if necessary, to convert the value of other to the cv-unqualified version of T.

Notes

Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.

If other is an rvalue expression, move constructor will be selected by overload resolution and called during copy-initialization.

Implicit conversion is defined in terms of copy-initialization: if an object of type T can be copy-initialized with expressionE, then E is implicitly convertible to T.

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.

 

 1 #include <string>
 2 #include <utility>
 3 #include <memory>
 4  
 5 int main()
 6 {
 7     std::string s = "test"; // OK: constructor is non-explicit
 8     std::string s2 = std::move(s); // this copy-initialization performs a move
 9  
10 //  std::unique_ptr<int> p = new int(1); // error: constructor is explicit
11     std::unique_ptr<int> p(new int(1)); // OK: direct-initialization
12  
13     int n = 3.14;    // floating-integral conversion
14     const int b = n; // const doesn't matter
15     int c = b;       // ...either way
16 }

See also

 

转载于:https://www.cnblogs.com/kira2will/p/3701552.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值