转: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
Tis 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.
- If
Tis a class type, and the type of other is different, or ifTis non-class type, but the type of other is a class type,user-defined conversion sequences 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 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
Tnor 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 的成员,返回指向第一个元素的迭代器.也是一个标准库函数,输入一个数字,返回指向该数字首元素的指针. 缓 ...
随机推荐
- js&jq 发送验证码倒计时
<input type="text" name='' id="btn"> //发送验证码倒计时var wait=30; function t ...
- Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 【20160924】GOCVHelper MFC增强算法(1)
//递归读取目录下全部文件(flag为r的时候递归) void getFiles(string path, vector<string>& files,string ...
- Windows上Python2.7安装Scrapy过程
需要执行: pip install scrapy pip install requests 在Windows下用pip安装Scrapy报如下错误,看错误提示就知道去http://aka.ms/vcpy ...
- phpcms 03
继续上次对header.html文件的分析 logo<a href="{siteurl($siteid)}/"><img src="{IMG_PATH} ...
- ContentProvider官方教程(1)何时用content provider
Content Providers Content providers manage access to a structured set of data. They encapsulate the ...
- Python 2.7.9 Demo - ini文件的读、写
ini文件 [weixin_info] hello = Nick Huang #coding=utf-8 #!/usr/bin/python import ConfigParser; cp = Con ...
- python3.5.1语法
1.print (变量名) print("字符串") 2.a=1 id(a)返回a在内存中的地址 3.可以用table弹出提示 #coding:utf-8 4.输入3/2 结果 ...
- linux终端下为什么用命令打开软件后,要关闭软件才能继续下一条命令?
用终端打开chromium浏览器(命令:chromium-browser)的时候发现打开浏览器之后无法继续在终端输入命令,只能关闭浏览器或者在终端按下Ctrl+c,此时系统将退出浏览器并可以继续在终端 ...
- jquery之clone()方法详解
clone()函数用于克隆当前匹配元素集合的一个副本,并以jQuery对象的形式返回.你也可以简单地理解为:克隆当前jQuery对象. 你还可以指定是否复制这些匹配元素(甚至它们的子元素)的附加数据( ...