首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

thread (thread&& x) noexcept; 这种构造函数如何用

2013-09-28 
thread (thread&& x) noexcept 这种构造函数怎么用?本帖最后由 shendaowu 于 2013-09-10 11:12:24 编辑ht

thread (thread&& x) noexcept; 这种构造函数怎么用?
本帖最后由 shendaowu 于 2013-09-10 11:12:24 编辑 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了
}

[解决办法]
引用:
Quote: 引用:

普通的构造或者operator=是 copy, paste的过程
move constructor 或 move operator= 是 cut, paste的过程。
用于处理临时变量的复制和赋值,效率更高了
怎么使用一个已存在的对象执行move constructor?就是那个&&。
比如说函数返回值

热点排行