博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
转:copy initialization
阅读量:5823 次
发布时间:2019-06-18

本文共 2952 字,大约阅读时间需要 9 分钟。

转自: 

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  , 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, 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  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.
  • Otherwise (if neither T nor the type of other are class types),  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,  will be selected by overload resolution and called during copy-initialization.

 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 
2 #include
3 #include
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
p = new int(1); // error: constructor is explicit11 std::unique_ptr
p(new int(1)); // OK: direct-initialization12 13 int n = 3.14; // floating-integral conversion14 const int b = n; // const doesn't matter15 int c = b; // ...either way16 }

See also

 

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

你可能感兴趣的文章
环境变量(总结)
查看>>
ios之UILabel
查看>>
Java基础之String,StringBuilder,StringBuffer
查看>>
1月9日学习内容整理:爬虫基本原理
查看>>
安卓中数据库的搭建与使用
查看>>
AT3908 Two Integers
查看>>
渐变色文字
查看>>
C++ 0X 新特性实例(比较常用的) (转)
查看>>
node生成自定义命令(yargs/commander)
查看>>
各种非算法模板
查看>>
node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)
查看>>
如何创建Servlet
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
win7 64位+Oracle 11g 64位下使用 PL/SQL Developer 的解决办法
查看>>
BZOJ1997:[HNOI2010]PLANAR——题解
查看>>
BZOJ1014:[JSOI2008]火星人prefix——题解
查看>>
使用Unity3D引擎开发赛车游戏
查看>>
HTML5新手入门指南
查看>>
opennebula 开发记录
查看>>
ubuntu 修改hostname
查看>>