建议增加自赋值检查,优化后的代码为: Array(const Array& other):n(other.n) { a = new int [n]; for (int i = 0; i < n; i++) { a[i] = other.a[i]; } } Array& operator=(const Array& other) { if (this != &other) { // 防止自赋值 delete[] a; // 释放原有内存 n = other.n; a = new int[n]; for (int i = 0; i < n; i++) { a[i] = other.a[i]; } } return *this; }
点赞

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务