运算符 = 模板中的重载 class
operator = overload in template class
我正在处理矩阵模板 class,现在我应该编写“=”运算符重载。
我想做的是删除出现在“=”左侧的矩阵,以及 return 等于出现在“=”右侧的矩阵的新矩阵。
因为我不能用distructor删除"this",所以我在函数中删除了"manually"。但现在我应该制作一个新矩阵,因此我制作了一个新矩阵 ("temp") 和 return。
问题是 "temp" 实际上是 return,但它没有设置在出现在 '=' 左侧的矩阵中。
代码:
Matrix<int> m (3, 4);
Matrix<int> m2(2, 5);
m2 = m;
这是主要部分。
函数:
template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& mat)
{
if (this==&mat)
{
return *this;
}
for (int i = 0; i < this->rows; i++)
{
delete[] this->mat[i];
}
delete[] this->mat;
Matrix<T> * temp = new Matrix<T>(mat.rows, mat.cols);
for (int i = 0; i < temp->rows; i++)
for (int j = 0; j < temp->cols; j++)
{
temp->mat[i][j] = mat.mat[i][j];
}
return *temp;
}
template<class T>
Matrix<T>::Matrix(int row, int col)
{
rows = row;
cols = col;
mat = new T*[rows];
for (int i = 0; i < rows; i++)
{
mat[i] = new T[cols];
}
rester(*this);
}
谢谢!!
使用std::vector
作为存储(而不是手动new
和delete
),只接受编译器生成的复制赋值运算符。就是这么简单。
如果你绝对想自己实现复制赋值,为了学习,那么就用复制构造来表达复制赋值。
为此,首先定义一个 noexcept
交换操作:
// In class definition:
friend
void swap( Matrix& a, Matrix& b )
noexcept
{
using std::swap;
// swap all data members here
}
那么复制赋值运算符可以简单的表示为
// In class definition
auto operator=( Matrix other )
-> Matrix&
{
swap( *this, other );
return *this;
}
它很流行,一个成语,因为它非常简单但异常安全。
而不是 return 引用,这会增加冗长和一些可能的边际效率低下而没有好处,您可能只想使用 void
作为 return 类型。但是,可能由于历史原因,标准库中的容器要求复制赋值运算符 return 对 self.
的引用
您需要为 this
分配内存,而不是创建 temp
。
template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& rhs)
{
if (this==&rhs)
{
return *this;
}
// Delete current memory
for (int i = 0; i < this->rows; i++)
{
delete[] this->mat[i];
}
delete[] this->mat;
this->rows = rhs.rows;
this->cols = rhs.cols;
// Allocate new memory
// Assign values to newly allocated memory.
this->mat = new int*[rhs.rows];
for (int = 0; i < rhs.rows; ++i )
{
this->mat[i] = new int[rhs.cols];
for (int j = 0; j < rhs.cols; j++)
{
this->mat[i][j] = rhs.mat[i][j];
}
}
// Return *this.
return *this;
}
我建议使用 中给出的建议。
也使用不同的参数名称。不要混淆成员变量 mat
和参数 mat
.
我正在处理矩阵模板 class,现在我应该编写“=”运算符重载。 我想做的是删除出现在“=”左侧的矩阵,以及 return 等于出现在“=”右侧的矩阵的新矩阵。
因为我不能用distructor删除"this",所以我在函数中删除了"manually"。但现在我应该制作一个新矩阵,因此我制作了一个新矩阵 ("temp") 和 return。 问题是 "temp" 实际上是 return,但它没有设置在出现在 '=' 左侧的矩阵中。
代码:
Matrix<int> m (3, 4);
Matrix<int> m2(2, 5);
m2 = m;
这是主要部分。
函数:
template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& mat)
{
if (this==&mat)
{
return *this;
}
for (int i = 0; i < this->rows; i++)
{
delete[] this->mat[i];
}
delete[] this->mat;
Matrix<T> * temp = new Matrix<T>(mat.rows, mat.cols);
for (int i = 0; i < temp->rows; i++)
for (int j = 0; j < temp->cols; j++)
{
temp->mat[i][j] = mat.mat[i][j];
}
return *temp;
}
template<class T>
Matrix<T>::Matrix(int row, int col)
{
rows = row;
cols = col;
mat = new T*[rows];
for (int i = 0; i < rows; i++)
{
mat[i] = new T[cols];
}
rester(*this);
}
谢谢!!
使用std::vector
作为存储(而不是手动new
和delete
),只接受编译器生成的复制赋值运算符。就是这么简单。
如果你绝对想自己实现复制赋值,为了学习,那么就用复制构造来表达复制赋值。
为此,首先定义一个 noexcept
交换操作:
// In class definition:
friend
void swap( Matrix& a, Matrix& b )
noexcept
{
using std::swap;
// swap all data members here
}
那么复制赋值运算符可以简单的表示为
// In class definition
auto operator=( Matrix other )
-> Matrix&
{
swap( *this, other );
return *this;
}
它很流行,一个成语,因为它非常简单但异常安全。
而不是 return 引用,这会增加冗长和一些可能的边际效率低下而没有好处,您可能只想使用 void
作为 return 类型。但是,可能由于历史原因,标准库中的容器要求复制赋值运算符 return 对 self.
您需要为 this
分配内存,而不是创建 temp
。
template<class T>
Matrix<T> & Matrix<T>::operator=(Matrix<T>& rhs)
{
if (this==&rhs)
{
return *this;
}
// Delete current memory
for (int i = 0; i < this->rows; i++)
{
delete[] this->mat[i];
}
delete[] this->mat;
this->rows = rhs.rows;
this->cols = rhs.cols;
// Allocate new memory
// Assign values to newly allocated memory.
this->mat = new int*[rhs.rows];
for (int = 0; i < rhs.rows; ++i )
{
this->mat[i] = new int[rhs.cols];
for (int j = 0; j < rhs.cols; j++)
{
this->mat[i][j] = rhs.mat[i][j];
}
}
// Return *this.
return *this;
}
我建议使用
也使用不同的参数名称。不要混淆成员变量 mat
和参数 mat
.