我正在尝试将一些部分从 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) 函数。
隐式转换函数和运算符函数的查找按什么顺序完成?有没有办法解决?