博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
现代c++之移动构造, 移动赋值, 拷贝构造, 拷贝赋值
阅读量:2388 次
发布时间:2019-05-10

本文共 2517 字,大约阅读时间需要 8 分钟。

#include 
#include
#include
#include
class CMyString {private: char * buf; int len;private: void copy(const char* s) { buf = new char[len+1]; memcpy(buf, s, len); buf[len] = '\0'; }public: CMyString() { std::cout << "构造函数" << std::endl; buf = nullptr; len = 0; } CMyString(const char* str = nullptr) { if (str == nullptr) { std::cout << "构造函数" << std::endl; buf = nullptr; len = 0; } else { std::cout << "构造函数" << str << std::endl; len = strlen(str); copy(str); } } CMyString(const CMyString& str) { std::cout << "拷贝构造函数" << str.buf << std::endl; len = str.len; copy(str.buf); } CMyString(CMyString&& str) { std::cout << "移动构造函数" << str.buf << std::endl; //也可以直接使用std::move len = str.len; buf = str.buf; str.len = 0; str.buf = nullptr; } CMyString& operator=(const CMyString& str) { std::cout << "拷贝赋值函数" << str.buf << std::endl; if (&str != this) { if (buf != nullptr) { delete[] buf; buf = nullptr; } len = str.len; copy(str.buf); } return *this; } CMyString& operator=(CMyString&& str) { std::cout << "移动赋值函数" << str.buf << std::endl; if (this != &str) { if (buf != nullptr) { delete[] buf; buf = nullptr; } len = str.len; buf = str.buf; str.len = 0; str.buf = nullptr; } return *this; } ~CMyString() { if (buf == nullptr) { std::cout << "析构函数" << std::endl; } else { std::cout << "析构函数" << buf << std::endl; delete[] buf; buf = nullptr; } } void print() { if (buf != nullptr) std::cout << buf << std::endl; else std::cout << "buf is null" << std::endl; } };void func1(CMyString str) { }CMyString func2() { CMyString s("34"); return s;}void test0() { CMyString s1("12"); func1(s1); //对象str尚未初始化,会调用拷贝构造函数 CMyString s2 = func2(); // 对象s2尚未初始化,会产生临时对象,调用移动构造函数 CMyString s3 = "56"; s3 = s1; //对象s3已初始化,会调用拷贝赋值函数}void test1() { CMyString s4 = "78"; std::vector
v1; //v1.push_back(s4); v1.push_back(std::move(s4)); // 对象尚未初始化,会调用移动构造函数 std::cout << "开始输出v1\n"; for (auto& str : v1) str.print(); std::vector
v2; v2 = std::move(v1); std::cout << "开始输出v1\n"; for (auto& str : v1) str.print(); std::cout << "开始输出v2\n"; for (auto& str : v2) str.print();}void test2() { CMyString s5 = "9"; s5 = func2(); // 对象s5已初始化, 会产生临时对象,调用移动赋值函数}int main(void){ std::cout << "begin test0()" << std::endl; test0(); std::cout << std::endl; std::cout << "begin test1()" << std::endl; test1(); std::cout << std::endl; std::cout << "begin test2()" << std::endl; test2(); return 0;}
你可能感兴趣的文章
linux下载edk2链接文件
查看>>
Win10家庭版DOCKER安装(上)
查看>>
Win10家庭版DOCKER安装(下)
查看>>
docker 图形化管理工具Kitematics
查看>>
unittest单元测试框架总结
查看>>
command 'x86_64-linux-gnu-gcc' failed with exit status 1
查看>>
浅谈前端SPA(单页面应用)
查看>>
Insecure default in Elasticsearch enables remote code execution
查看>>
how to use this bugs unserialize()
查看>>
PHP5 Globals Vulnerability
查看>>
关于php包含Apache日志的随想
查看>>
Grep与web漏洞挖掘
查看>>
正则表达式使用详解
查看>>
引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法(新手推荐)
查看>>
编写不受魔术引号影响的php应用
查看>>
PHP开发安全设置
查看>>
Php Endangers - Remote Code Execution
查看>>
变量的变量,PHP和你
查看>>
PROC系列之四---/proc/loadavg
查看>>
某大型网站的内核TCP/ip优化脚本
查看>>