My test program from learning "move constructor", "move assignment". For personal memo purpose.
来源:互联网 发布:淘宝全屏海报显示不全 编辑:程序博客网 时间:2024/05/20 04:50
#include <iostream>#include <string>/*When it comes to move assignment, we cannot just use std::swap(*this, rhs);It is because std::swap combines with three std::move commands: one is a moveconstructor, the other two are move assignments. So it will result in endlessrecursion from call move assignment within move assignment.*/using std::cin; using std::cout; using std::endl;class MetaData{public: MetaData (int size, const std::string& name) : _name( name ) , _size( size ) { cout << "MetaData initialiser!" << endl; } // copy constructor MetaData (const MetaData& other) : _name( other._name ) , _size( other._size ) { cout << "MetaData copy constructor!" << endl; } // move constructor MetaData (MetaData&& other) : _name( std::move(other._name) ) , _size( other._size ) { cout << "MetaData move constructor!" << endl; }MetaData &operator= (MetaData &other){if(this != &other){_name = other._name;_size = other._size;}cout << "MetaData copy assignment!" << endl;return *this;} MetaData &operator= (MetaData &&other){if(this != &other){_name = other._name;_size = other._size;other._name = "";other._size = 0;}cout << "MetaData move assignment!" << endl;return *this;} std::string getName () const { return _name; } int getSize () const { return _size; }private: std::string _name; int _size;};class ArrayWrapper{public: // default constructor produces a moderately sized array ArrayWrapper () : _p_vals( new int[ 64 ] ) , _metadata( 64, "ArrayWrapper" ) {} ArrayWrapper (int n) : _p_vals( new int[ n ] ) , _metadata( n, "ArrayWrapper" ) {} // move constructor ArrayWrapper (ArrayWrapper&& other) : _p_vals( other._p_vals ) , _metadata( std::move(other._metadata) ) { cout << "ArrayWrapper move constructor!" << endl;other._p_vals = NULL; } // copy constructor ArrayWrapper (const ArrayWrapper& other) : _p_vals( new int[ other._metadata.getSize() ] ) , _metadata( other._metadata ) { for ( int i = 0; i < _metadata.getSize(); ++i ) { _p_vals[ i ] = other._p_vals[ i ]; } } ~ArrayWrapper () { delete [] _p_vals; }private: int *_p_vals; MetaData _metadata;};class Integer{public:Integer(int n, std::string s):elem(n),str(s){cout << "Integer initialiser!" << endl;}Integer(const Integer &val):elem(val.elem){str = "COPY";cout << "Integer copy constructor!" << endl;}Integer(Integer &&val):elem(val.elem){str = "MOVE";cout << "Integer move constructor!" << endl;}Integer &operator=(const Integer &val){cout << "****** Integer copy assignment begins! ******" << endl;// if(this != &val)// {// elem = val.elem;// str = val.str;// cout << "Integer copy assignment!" << endl;// }Integer copy = val;std::swap(*this, copy);cout << "********* Integer copy assignment ends *******" << endl;return *this;}Integer &operator=(Integer &&val){cout << "******* Integer move assignment begins! *******" << endl;std::swap(elem, val.elem);std::swap(str, val.str);cout << "******* Integer move assignment ends! *******" << endl;return *this;}std::string getVal(){return str;}private:int elem;std::string str;};Integer ret(){Integer tmp(100, "tmp");return tmp;}int main(){// ArrayWrapper aw1;// ArrayWrapper aw3(aw1);Integer i1(99,"i1");Integer i2(100,"i2");// cout << i2.getVal() << endl;i2 = {1, "haha"};cout << endl;i2= i1;cout << i2.getVal() << endl;return 0;}
0 0
- My test program from learning "move constructor", "move assignment". For personal memo purpose.
- c++中move constructor and assignment
- C++11 Tutorial: Introducing the Move Constructor and the Move Assignment Operator
- 函数返回语句与copy (move) constructor, copy (move) assignment operator的关系
- 关于Move constructor
- C++11: Move Semantics (Move Constructor)
- 右值引用、move与move constructor
- Move
- Move
- Move
- Move..
- [Memo]From My QQ
- only for test purpose
- [c++11]std::move test
- Move!Move!!Move!!!
- Finish the move in my lab
- leetcode283. [Array]Move Zeroes My Submissions Question
- Good link for english learning purpose.
- Linux系统函数write(strlen、sizeof与write结合使用的区别)
- 黑马程序员——ios开发基础之OC中的Foundation框架(一)
- Java基础——switch、循环语句、控制跳转语句
- Android 自定义九宫格控件Gridview用法以及属性
- Node.js开发入门—UDP编程
- My test program from learning "move constructor", "move assignment". For personal memo purpose.
- 伍子胥
- springmvc文件上传的三种方式
- 夫差
- 大学里,应该多一份大方
- 排序算法之希尔排序
- 使用awk命令向集群中其他机器拷贝文件
- 【HAOI2012】【BZOJ2751】容易题(easy)
- 32位ubuntu14.04手动编译hadoop2.6.0