c++ std::move详解
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详解相关推荐
- C++11 并发指南三(std::mutex 详解)
上一篇<C++11 并发指南二(std::thread 详解)>中主要讲到了 std::thread 的一些用法,并给出了两个小例子,本文将介绍 std::mutex 的用法. Mutex ...
- 【转】C++11 并发指南五(std::condition_variable 详解)
http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...
- C++11 并发指南五(std::condition_variable 详解)
前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- 【C/C++开发】C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- std::thread详解
转自:http://www.cnblogs.com/haippy/p/3236136.html 上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thr ...
- C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- std::shared_ptr 详解
一.介绍 shared_ptr是一种智能指针(smart pointer),作用有如同指针,但会记录有多少个shared_ptrs共同指向一个对象.这便是所谓的引用计数(reference count ...
- std::tie详解
std::tie:创建左值引用的 tuple,或将 tuple 解包为独立对象 返回值 含左值引用的 std::tuple 对象. 注意 std::tie 可用于解包 std::pair ,因为 st ...
最新文章
- php 将数组导出excel,#php 怎样将 数组导出excel文件#前端导出excel表格
- python【蓝桥杯vip练习题库】ADV-150 周期字串
- 全卷积神经网络( FCN ):语义分割深度模型先驱
- Linux 标准目录结构 FHS
- CRMEB支付状态未改变
- c语言作业帮,少训练大功课C语言
- 算法洗脑系列(8篇)——第八篇 概率思想
- 【PostgreSQL-9.6.3】函数(3)--日期和时间函数
- RRC 常用名词解释
- 常见软件非功能性需求描述案例
- 2022年8种高级威胁预测出炉、FBI就零日漏洞发出警报|11月22日全球网络安全热点
- AOP设计与原理(连接点,切入点与代理模式)——Spring AOP(二)
- 深度学习论文翻译解析(二十):YOLOv4: Optimal Speed and Accuracy of Object Detection
- Github项目:AI消除马赛克实战
- 职言 | 单纯做业务测试真的行得通吗?
- 【AR\VR开发基础】GLSL初步学习①
- UNIX2DOS/DOS2UNIX for Windows
- 北京量子院量子科学论坛:文凯博士介绍相干量子计算
- 测试人遇到被测 APP 要下架,怎么处理?
- 获取联系人的电话号码