【发布时间】:2010-10-21 14:11:45
【问题描述】:
在我看来,这不仅仅是我的问题。
请不要重复关闭我的问题,因为我查看了这些问题但没有找到解决方案。
class Matrix<T> {
private Int32 rows;
private Int32 cols;
private T[,] matrix;
public Matrix() {
rows = cols = 0;
matrix = null;
}
public Matrix(Int32 m, Int32 n) {
rows = m;
cols = n;
matrix = new T[m, n];
}
public T this[Int32 i, Int32 j] {
get {
if (i < rows && j < cols)
return matrix[i, j];
else
throw new ArgumentOutOfRangeException();
}
protected set {
if (i < rows && j < cols)
matrix[i, j] = value;
else
throw new ArgumentOutOfRangeException();
}
}
public Int32 Rows {
get { return rows; }
}
public Int32 Cols {
get { return cols; }
}
public static Matrix<T> operator+(Matrix<T> a, Matrix<T> b) {
if(a.cols == b.cols && a.rows == b.rows) {
Matrix<T> result = new Matrix<T>(a.rows, a.cols);
for (Int32 i = 0; i < result.rows; ++i)
for (Int32 j = 0; j < result.cols; ++j)
result[i, j] = a[i, j] + b[i, j];
return result;
}
else
throw new ArgumentException("Matrixes don`t match operator+ requirements!");
}
public static Matrix<T> operator-(Matrix<T> a, Matrix<T> b) {
if (a.cols == b.cols && a.rows == b.rows) {
Matrix<T> result = new Matrix<T>(a.rows, a.cols);
for (Int32 i = 0; i < result.rows; ++i)
for (Int32 j = 0; j < result.cols; ++j)
result[i, j] = a[i, j] - b[i, j];
return result;
}
else
throw new ArgumentException("Matrixes don`t match operator- requirements!");
}
你知道编译器告诉什么:“运算符'-'不能应用于'T'和'T'类型的操作数”,这适用于所有运算符。
那么,对此最好的决定是什么?据我所知,接口不能包含运算符,因此唯一的方法是 T 类型的抽象基类。而且,正如我发现的那样,操作符不能定义为抽象的。
【问题讨论】:
-
@ShuggyCoUk - 实际上,
dynamic(在 C# 4.0 中)确实支持运算符。所以这会工作,但会更慢。 -
对不起 - 我应该说得更清楚。 Dynamic 仍然不允许您对泛型类型执行此操作,它允许您将 cast 转换为动态 then 对任何支持它的操作执行操作。如果最终用户只是想使用正确的运算符(放弃所有编译检查),那么这很好。如果他们想获得与原语或结构(可能的情况)的性能相近的东西,他们不会。
标签: c# generics operator-overloading