假設我已經實現了一個Vector類。在C++中,可以通過在全局範圍內重載operator *做自然的數學表達式「縮放」:浮動倍數紅寶石矢量
template <typename T> // T can be int, double, complex<>, etc.
Vector operator*(const T& t, const Vector& v);
template <typename T> // T can be int, double, complex<>, etc.
Vector operator*(const Vector& v, const T& t);
然而,當它去紅寶石,因爲參數沒有輸入,就可以寫
class Vector
def *(another)
case another
when Vector then ...
when Numeric then ...
end
end
end
這允許Vector * Numeric而不是Numeric * Vector。有解決它的方法嗎?