【发布时间】:2013-12-09 03:53:48
【问题描述】:
我以这种方式声明了一个模板化的Matrix 类:
template<typename Type> class Matrix {
// Some code for matrix computations
}
现在,我正在尝试重载operator+,以保证结果会是更大的类型。我正在尝试这个东西:
template<typename OtherType>
Matrix<Type> operator+ (Matrix<OtherType> mat) {
// Dimension check and matrix addition code
}
但是这样做,我实际上会强制 C++ 选择 Matrix<Type> 作为返回类型。我想要实现的是,例如,Matrix<int> + Matrix<float> 将导致Matrix<float>。
关于如何做到这一点的任何建议?
【问题讨论】:
-
floats 不能存储每个int值。
标签: c++ templates types matrix return