简单的实现:
class Myclass {
public:
Myclass(size_t size) : data_(size ? new(std::nothrow) int[size] : nullptr) , size_(!data_ ? 0 : size){}
Myclass(const Myclass& other) {
data_ = other.size_ ? new(std::nothrow) int[other.size_] : nullptr;
size_ = !data_ ? 0 : other.size_;
}
Myclass& operator=(Myclass other) {
Swap(other);
return *this;
}
void Swap(Myclass& other) noexcept {
std::swap(data_, other.data_);
std::swap(size_, other.size_);
}
private:
int* data_;
size_t size_;
};