转: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:
The effects of copy initialization are:
- If
T
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.
- If
T
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,user-defined conversion sequences 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 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 ofT
.
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.
#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()); // OK: direct-initialization int n = 3.14; // floating-integral conversion
const int b = n; // const doesn't matter
int c = b; // ...either way
}
See also
转:copy initialization的更多相关文章
- C++ 之 Direct and Copy Forms of Initialization
Extraction from C++ Primer 5th. Editioin 3.2.1 C++ has several different forms of initialization, we ...
- C++-copy constructor、copy-assignment operator、destructor
本文由@呆代待殆原创,转载请注明出处. 对于一个类来说,我们把copy constructor.copy-assignment operator.move constructor.move-assig ...
- [C++] Copy Control (part 1)
Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...
- Copy elision in C++
Copy elision (or Copy omission) is a compiler optimization technique that avoids unnecessary copying ...
- C++ Primer 随笔 Chapter 13 复制控制
1.复制控制包含的内容:复制构造函数.赋值操作符.析构函数 2.复制构造函数: a. 定义:只有单个形参,而且该形参是对本类类型的引用,这样的构造函数被成为复制构造函数 b. 适用情况: (1)根据一 ...
- [C++] String Basic
Namespace Declarations A using declaration let us use a name from a namespace without qualify the na ...
- 关于c++的string的operator =
在 c++ primer 5 中在说到string的章节里面有这样一句话: string s5 = "hiya"; // copy initialization 也就是说,这里说上 ...
- complexType
//decltype的表达式如果是加上括号的变量,结果将是引用 decltype((variable)) ruiy; //此变量的数据类型是引用(但此处变量的申明语句是错误的,引用不是对象,指向的对象 ...
- <<C++ Primer>> 第三章 字符串, 向量和数组 术语表
术语表 第 3 章 字符串, 向量和数组 begin: 是 string 和 vector 的成员,返回指向第一个元素的迭代器.也是一个标准库函数,输入一个数字,返回指向该数字首元素的指针. 缓 ...
随机推荐
- YTU 2974: C语言习题5.26--文件操作3
2974: C语言习题5.26--文件操作3 时间限制: 1 Sec 内存限制: 128 MB 提交: 213 解决: 92 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编号 ...
- windows上安装apache python mod_python
综述: windows上安装apache python mod_python的例子.教程甚至图解都不少:但作为新手还是会出错,而且一时无法快速排解. 在此笔者将根据自己的实践经验,给出几个需要注意 ...
- jqeury之平移轮播
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 巧用ViewPager 打造不一样的广告轮播切换效果
一.概述 如果大家关注了我的微信公众号的话,一定知道我在5月6号的时候推送了一篇文章,文章名为Android超高仿QQ附近的人搜索展示(一),通过该文可以利用ViewPager实现单页显示多个Item ...
- qbxt十一系列二
PA[题目描述]汉诺塔升级了:现在我们有N个圆盘和N个柱子,每个圆盘大小都不一样,大的圆盘不能放在小的圆盘上面,N个柱子从左到右排成一排.每次你可以将一个柱子上的最上面的圆盘移动到右边或者左边的柱子上 ...
- CentOS6 启动流程图文解剖
我们在使用Linux操作系统的时候,我们只需按下电源键,等待,然后输入账户和密码就可以使用Linux操作系统了.那么在按下电源到输入账号和密码之前,操作系统都做了些什么?下面就来讲述在这段时间发生的动 ...
- 十一、Swing
1.Swing常用窗体 (1)JFrame框架窗体 JFrame窗体是一个容器,是Swing程序中各组件的载体. 语法:JFrame jf = new JFrame(title); Container ...
- Using SYSTEM.MOUSE_ITEM In Oracle Forms
If the mouse is in an item, SYSTEM.MOUSE_ITEM represents the name of that item as a CHAR value.For e ...
- Creating Timer in Oracle D2k / Forms 6i and Displaying a Clock
Creating Timer in Oracle D2k / Forms 6i and Displaying a Clock This is about timer in D2k An externa ...
- .Net需要掌握的知识
一.C#开发 1.C#基础 变量定义 如何变量的初始化 变量的作用域 常量 字符串处理 使用正则表达式 什么是CTS类型?数据类型如何分类以及各个数据类型范围 类型的转化分类 显式转换何隐式转化如何区 ...