假设我有一个(不可变的)矩阵类,它在构造函数中动态创建一个数组,并在解构函数中删除它。
您违反了Rule of Three,因为没有实现复制构造函数(C++11 中的Rule of Five,因为没有实现移动构造函数和移动赋值运算符)。
您的复制赋值运算符存在内存泄漏,因为在分配 new[]'ed 数组之前,它不是 delete[]'ing 旧的 data 数组。
试试这个:
template <typename T>
class matrix {
private:
T* data;
size_t const rows, cols;
public:
matrix(size_t rows, size_t cols) : rows(rows), cols(cols) {
data = new T[rows*cols];
}
matrix(const matrix<T> &src) : rows(src.rows), cols(src.cols) {
data = new T[rows*cols];
std::copy(data, &data[rows*cols], src.data);
}
/* for C++11:
matrix(matrix<T> &&src) : rows(0), cols(0), data(nullptr) {
std::swap(rows, src.rows);
std::swap(cols, src.cols);
std::swap(data, src.data);
}
*/
~matrix() {
delete [] data;
}
T& operator()(size_t row, size_t col) {
return data[(row * cols) + col];
}
T operator()(size_t row, size_t col) const {
return data[(row * cols) + col];
}
matrix<T>& operator=(const matrix<T>& other) {
if (&other != this) {
delete[] data;
rows = other.rows;
cols = other.cols;
data = new T[rows*cols];
std::copy(data, &data[rows*cols], other.data);
}
return *this;
}
/* for C++11:
matrix<T>& operator=(matrix<T> &&other) {
delete[] data;
data = nullptr;
rows = cols = 0;
std::swap(rows, other.rows);
std::swap(cols, other.cols);
std::swap(data, other.data);
return *this;
}
*/
};
但是,copy-and-swap idiom 会更安全:
template <typename T>
class matrix {
private:
T* data;
size_t const rows, cols;
public:
matrix(size_t rows, size_t cols) : rows(rows), cols(cols) {
data = new T[rows*cols];
}
matrix(const matrix<T> &src) : rows(src.rows), cols(src.cols) {
data = new T[rows*cols];
std::copy(data, &data[rows*cols], src.data);
}
/* for C++11:
matrix(matrix<T> &&src) : rows(0), cols(0), data(nullptr) {
src.swap(*this);
}
*/
~matrix() {
delete [] data;
}
T& operator()(size_t row, size_t col) {
return data[(row * cols) + col];
}
T operator()(size_t row, size_t col) const {
return data[(row * cols) + col];
}
void swap(matrix<T>& other) noexcept
{
std::swap(rows, other.rows);
std::swap(cols, other.cols);
std::swap(data, other.data);
}
matrix<T>& operator=(const matrix<T>& other) {
if (&other != this) {
matrix<T>(other).swap(*this);
}
return *this;
}
/* for C++11:
matrix<T>& operator=(matrix<T> &&other) {
other.swap(*this);
return *this;
}
*/
};
在后一种情况下,复制赋值和移动赋值运算符可以在 C++11 中合并为一个运算符:
matrix<T>& operator=(matrix<T> other) {
other.swap(*this);
return *this;
}
或者,您可以使用std::vector 遵循零规则,让编译器和 STL 为您完成所有工作:
template <typename T>
class matrix {
private:
std::vector<T> data;
size_t const rows, cols;
public:
matrix(size_t rows, size_t cols) : rows(rows), cols(cols), data(rows*cols) {
}
T& operator()(size_t row, size_t col) {
return data[(row * cols) + col];
}
T operator()(size_t row, size_t col) const {
return data[(row * cols) + col];
}
};
因为我在operator= 函数中没有默认构造函数,所以这里的数据只是垃圾,对吧?
不能,因为operator= 只能在以前构造的对象上调用,就像任何其他类实例方法一样。
即使我有一个默认构造函数,它会被调用吗?
在你展示的例子中,没有。
我想澄清一下,我只关心这样的电话:
matrix<int> A = B;
该语句根本不调用operator=。 = 的使用只是语法糖,编译器实际上执行复制构造,就好像你已经写了这个:
matrix<int> A(B);
这需要一个您尚未实现的复制构造函数,并且编译器生成的复制构造函数不足以对您的数组进行深层复制。
复制分配看起来更像这样:
matrix<int> A; // <-- default construction
A = B; // <-- copy assignment
matrix<int> A(B); // <-- copy construction
A = C; // <-- copy assignment