标签:c++
在一个类的内部同时实现常规拷贝构造函数和移动拷贝构造函数,常规赋值函数和移动赋值函数。
调用时若参数为一个左值,则调用常规函数;若参数为一个右值,则调用移动函数。
也可调用"std::move"强行调用移动函数。
#include <iostream> #include <utility> using std::cout; using std::endl; class Useless { private: int n; // number of elements char * pc; // pointer to data static int ct; // number of objects public: Useless(); explicit Useless(int k); Useless(int k, char ch); Useless(const Useless & f); // regular copy constructor Useless(Useless && f); // move constructor ~Useless(); Useless operator+(const Useless & f)const; Useless & operator=(const Useless & f); // copy assignment Useless & operator=(Useless && f); // move assignment void ShowObject() const; }; // implementation int Useless::ct = 0; Useless::Useless() { cout << "enter " << __func__ << "()\n"; ++ct; n = 0; pc = nullptr; ShowObject(); cout << "leave " << __func__ << "()\n"; } Useless::Useless(int k) : n(k) { cout << "enter " << __func__ << "(k)\n"; ++ct; pc = new char[n]; ShowObject(); cout << "leave " << __func__ << "(k)\n"; } Useless::Useless(int k, char ch) : n(k) { cout << "enter " << __func__ << "(k, ch)\n"; ++ct; pc = new char[n]; for (int i = 0; i < n; i++) pc[i] = ch; ShowObject(); cout << "leave " << __func__ << "(k, ch)\n"; } Useless::Useless(const Useless & f): n(f.n) { cout << "enter " << __func__ << "(const &)\n"; ++ct; pc = new char[n]; for (int i = 0; i < n; i++) pc[i] = f.pc[i]; ShowObject(); cout << "leave " << __func__ << "(const &)\n"; } Useless::Useless(Useless && f): n(f.n) { cout << "enter " << __func__ << "(&&)\n"; ++ct; pc = f.pc; // steal address f.pc = nullptr; // give old object nothing in return f.n = 0; ShowObject(); f.ShowObject(); cout << "leave " << __func__ << "(&&)\n"; } Useless::~Useless() { cout << "enter " << __func__ << "()\n"; ShowObject(); --ct; delete [] pc; cout << "leave " << __func__ << "()\n"; } Useless & Useless::operator=(const Useless & f) // copy assignment { cout << "enter " << __func__ << "(const &)\n"; ShowObject(); f.ShowObject(); if (this == &f) return *this; delete [] pc; n = f.n; pc = new char[n]; for (int i = 0; i < n; i++) pc[i] = f.pc[i]; ShowObject(); f.ShowObject(); cout << "leave " << __func__ << "(const &)\n"; return *this; } Useless & Useless::operator=(Useless && f) // move assignment { cout << "enter " << __func__ << "(&&)\n"; ShowObject(); f.ShowObject(); if (this == &f) return *this; delete [] pc; n = f.n; pc = f.pc; f.n = 0; f.pc = nullptr; ShowObject(); f.ShowObject(); cout << "leave " << __func__ << "(&&)\n"; return *this; } Useless Useless::operator+(const Useless & f)const { cout << "enter " << __func__ << "(const &)\n"; ShowObject(); f.ShowObject(); Useless temp = Useless(n + f.n); for (int i = 0; i < n; i++) temp.pc[i] = pc[i]; for (int i = n; i < temp.n; i++) temp.pc[i] = f.pc[i - n]; cout << "\t temp: "; temp.ShowObject(); cout << "leave " << __func__ << "(const &)\n"; return temp; } void Useless::ShowObject() const { cout << "\t this=" << this << ", ct=" << ct; cout << ", pc=(" << n << ", " << (void*)pc << ", "; if (n == 0) cout << "(object empty)"; else for (int i = 0; i < n; i++) cout << pc[i]; cout << endl; } // application int main() { Useless one(10, ‘x‘); Useless two = one + one; // calls move constructor cout << "object one:\n"; one.ShowObject(); cout << "object two:\n"; two.ShowObject(); Useless three, four; cout << "three = one\n"; three = one; // automatic copy assignment cout << "now object three:\n"; three.ShowObject(); cout << "and object one:\n"; one.ShowObject(); cout << "four = one + two\n"; four = one + two; // automatic move assignment cout << "now object four:\n"; four.ShowObject(); cout << "four = move(one)\n"; four = std::move(one); // forced move assignment cout << "now object four:\n"; four.ShowObject(); cout << "and object one:\n"; one.ShowObject(); std::cin.get(); }
测试结果如下。红色部分不是测试结果的一部分,是对测试结果的分析。
enter Useless(k, ch) //退出"main()"时,析构栈空间的对象。析构顺序与构造顺序相反。 |
本文出自 “用C++写诗” 博客,谢绝转载!
标签:c++
原文地址:http://frankniefaquan.blog.51cto.com/12613979/1934754