thread (thread&& x) noexcept; 这种构造函数如何用
thread (thread&& x) noexcept; 这种构造函数怎么用?
http://www.cplusplus.com/reference/thread/thread/thread/
move constructor的中文名是什么?转移构造函数还是转换构造函数?
另外求解释一下这个move constructor。
------解决方案--------------------
普通的构造或者operator=是 copy, paste的过程
move constructor 或 move operator= 是 cut, paste的过程。
用于处理临时变量的复制和赋值,效率更高了
------解决方案--------------------
move constructor翻译成移动构造比较多
转移也差不多
(4) move constructor
Construct a thread object that acquires the thread of execution represented by x (if any). This operation does not affect the execution of the moved thread in any way, it simply transfers its handler.
The x object no longer represents any thread of execution.
这里不是解释了吗?
效果是把x管理的线程管理权转移过来,并对线程本身不造成影响,转移后的x不再管理任何线程
------解决方案--------------------
用个简单的例子
假如class A有个成员变量 char* str,是构造的时候new,析构的时候delete
普通的拷贝构造是new一个str,把参数中的对象的str再strcpy过来。
move constructor这样做:
------解决方案--------------------
比如说函数返回值
http://www.cplusplus.com/reference/thread/thread/thread/
move constructor的中文名是什么?转移构造函数还是转换构造函数?
另外求解释一下这个move constructor。
------解决方案--------------------
普通的构造或者operator=是 copy, paste的过程
move constructor 或 move operator= 是 cut, paste的过程。
用于处理临时变量的复制和赋值,效率更高了
------解决方案--------------------
move constructor翻译成移动构造比较多
转移也差不多
(4) move constructor
Construct a thread object that acquires the thread of execution represented by x (if any). This operation does not affect the execution of the moved thread in any way, it simply transfers its handler.
The x object no longer represents any thread of execution.
这里不是解释了吗?
效果是把x管理的线程管理权转移过来,并对线程本身不造成影响,转移后的x不再管理任何线程
------解决方案--------------------
用个简单的例子
假如class A有个成员变量 char* str,是构造的时候new,析构的时候delete
普通的拷贝构造是new一个str,把参数中的对象的str再strcpy过来。
move constructor这样做:
A (A&& a) // 不是const A& a
{
str = a.str; //把a的str偷过来
a.str = nullptr; //赋值空指针,不然 a析构就把str也delete了
}
------解决方案--------------------
比如说函数返回值