class Buffer { public: explicit Buffer(int capacity) : capacity_(capacity), len_(0), buff_(new char[capacity] {0}) { std::cout << "默认的构造函数" << std::endl; }; ~Buffer() {}; Buffer(const Buffer& other) noexcept : capacity_(other.capacity_), len_(other.len_), buff_(capacity_ ? new char[capacity_] {0} : nullptr) { if (capacity_) { std::copy(other.buff_, other.buff_ + other.capacity_, this->buff_); } std::cout << "拷贝构造" << std::endl; } Buffer& operator=(Buffer other) noexcept // { std::cout << "赋值拷贝" << std::endl; Swap(*this, other); return *this; } Buffer(Buffer&& other) noexcept: capacity_(0), len_(0), buff_(nullptr) { Swap(*this, other); std::cout << "移动构造" << std::endl; } static void Swap(Buffer& lns, Buffer& rhs) noexcept { std::swap(lns.buff_, rhs.buff_); std::swap(lns.capacity_, rhs.capacity_); std::swap(lns.len_, rhs.len_); } private: int capacity_; int len_; char* buff_; };
标签:std,capacity,cout,18,构造,buff,赋值 From: https://www.cnblogs.com/zwj-199306231519/p/17987149