【发布时间】:2010-10-23 11:00:38
【问题描述】:
我正在尝试将一些部分从 ginac (www.ginac.de) 移植到 C#。但是我遇到了这个:
class Program {
static void Main(string[] args) {
symbol s = new symbol();
numeric n = new numeric();
ex e = s + n; // "Operator + doesn't work for symbol, numeric"
}
}
class ex {
//this should be already be sufficient:
public static implicit operator ex(basic b) {
return new ex();
}
//but those doesn't work as well:
public static implicit operator ex(numeric b) {
return new ex();
}
public static implicit operator ex(symbol b) {
return new ex();
}
public static ex operator +(ex lh, ex rh) {
return new ex();
}
}
class basic {
}
class symbol : basic {
}
class numeric : basic {
}
正确的顺序应该是:隐式转换symbol->basic->ex,然后numeric->basic->ex再使用ex operator+(ex,ex)函数。
隐式转换函数和运算符函数的查找按什么顺序完成? 有没有办法解决这个问题?
【问题讨论】:
标签: c# casting operator-overloading implicit-conversion ginac