【发布时间】:2023-03-03 02:10:01
【问题描述】:
我正在制作一个矩阵库,我希望能够做类似的事情
Matrix<double> = Matrix<int> + Matrix<double>
这可能吗?
这是我现在拥有的:
template<typename T>
Matrix<T>& Matrix<T>::operator+(const Matrix& rhs) {
if(w != rhs.w || h != rhs.h) {
printf("\e[31m[ERROR] Arithmetic Error: attempted to add %dx%d matrix to %dx%d matrix\e[0m\n", w, h, rhs.w, rhs.h);
}
else {
for(uint32_t i = 0;i < size;++i) {
m[i] += rhs.m[i];
}
}
return *this;
}
【问题讨论】:
-
operator+改变其操作数很奇怪,它看起来更像operator +=而不是operator+。
标签: c++ templates operator-overloading