c++ std::move详解

在移动构造中的时候,移动拷贝其实就是把原来参数对象的指针地址给到了构造的对象,再把原先对象的指针置为NULL,这样内存就不会被原来函数给析构了。对于实体的对象执行的其实也是拷贝构造。

&&是可以直接对目标进行直接操作的,对操作者是可见的,不是副本

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;class Str {public:char *str;Str(char value[]) {cout << "普通构造函数..." << endl;str = NULL;int len = strlen(value);str = (char *)malloc(len + 1);memset(str, 0, len + 1);strcpy(str, value);}Str(const Str &s) {cout << "拷贝构造函数..." << endl;str = NULL;int len = strlen(s.str);str = (char *)malloc(len + 1);memset(str, 0, len + 1);strcpy(str, s.str);}Str(Str &&s) {cout << "移动构造函数..." << endl;str = NULL;str = s.str;s.str = NULL;}~Str() {cout << "析构函数" << endl;if (str != NULL) {free(str);str = NULL;}}
};class f {public:f() {}explicit f(int &&a) {a = a + 1;b = a;}~f() {}void get() { std::cout << "value: " << b << std::endl; }private:int b;
};int main() {char value[] = "I love zx";Str s(value);vector<Str> vs;vs.push_back(move(s));//   vs.push_back(s);cout << vs[0].str << endl;if (s.str != NULL) cout << s.str << endl;// return 0;int a = 10;int b = std::move(a);std::string c("hello");std::string d = std::move(c);// &a = NULL;f object(std::move(a));std::cout << " a: " << a << std::endl;object.get();std::cout << " b: " << b << std::endl;std::cout << " c: " << c << std::endl;std::cout << " d: " << d << std::endl;
}

结果

普通构造函数...
移动构造函数...
I love zxa: 11
value: 11b: 10c: d: hello
析构函数
析构函数

参考链接

c++ std::move详解相关推荐

  1. C++11 并发指南三(std::mutex 详解)

    上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...

  2. 【转】C++11 并发指南五(std::condition_variable 详解)

    http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...

  3. C++11 并发指南五(std::condition_variable 详解)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  4. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  5. 【C/C++开发】C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  6. std::thread详解

    转自:http://www.cnblogs.com/haippy/p/3236136.html 上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thr ...

  7. C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  8. std::shared_ptr 详解

    一.介绍 shared_ptr是一种智能指针(smart pointer),作用有如同指针,但会记录有多少个shared_ptrs共同指向一个对象.这便是所谓的引用计数(reference count ...

  9. std::tie详解

    std::tie:创建左值引用的 tuple,或将 tuple 解包为独立对象 返回值 含左值引用的 std::tuple 对象. 注意 std::tie 可用于解包 std::pair ,因为 st ...

最新文章

  1. php 将数组导出excel,#php 怎样将 数组导出excel文件#前端导出excel表格
  2. python【蓝桥杯vip练习题库】ADV-150 周期字串
  3. 全卷积神经网络( FCN ):语义分割深度模型先驱
  4. Linux 标准目录结构 FHS
  5. CRMEB支付状态未改变
  6. c语言作业帮,少训练大功课C语言
  7. 算法洗脑系列(8篇)——第八篇 概率思想
  8. 【PostgreSQL-9.6.3】函数(3)--日期和时间函数
  9. RRC 常用名词解释
  10. 常见软件非功能性需求描述案例
  11. 2022年8种高级威胁预测出炉、FBI就零日漏洞发出警报|11月22日全球网络安全热点
  12. AOP设计与原理(连接点,切入点与代理模式)——Spring AOP(二)
  13. 深度学习论文翻译解析(二十):YOLOv4: Optimal Speed and Accuracy of Object Detection
  14. Github项目:AI消除马赛克实战
  15. 职言 | 单纯做业务测试真的行得通吗?
  16. 【AR\VR开发基础】GLSL初步学习①
  17. UNIX2DOS/DOS2UNIX for Windows
  18. 北京量子院量子科学论坛:文凯博士介绍相干量子计算
  19. 测试人遇到被测 APP 要下架,怎么处理?
  20. 获取联系人的电话号码

热门文章

  1. SAP SD之发货和运输
  2. 研发管理三部曲——壹 · 研发对于IT公司来说的核心价值是什么
  3. Chocolatey安装失败
  4. HBase WAL 解析
  5. CodeForces - 622E Ants in Leaves 贪心
  6. 操作系统---文件的空闲管理
  7. win10开启热点时报错“无法设置移动热点”
  8. OpenWrt之时区设置(夏令时设置)
  9. MSIL权威指南阅读 - CLR基础
  10. C++运算符重载中有些方法为什么需要定义为友元函数