![](/img/trans.png)
[英]How to overload the arrow dereference operator->() not for solid objects but for pointers
[英]Declaration of overload operator- of objects with 2 different types
我有一個模板類並編寫了一個運算符-該運算符應使用2個不同類型的對象進行數學運算並將其作為新對象返回。 現在我想知道如何在類中聲明此運算符,因為類似
template<typename S>
friend auto operator-(const XY<T>,const XY<S>)->XY<decltype(T-S)>;
不允許嗎? 這是我的示例代碼:
template <typename T>
class XY{
private:
T x
...
// template<typename S>
// friend auto operator-(const XY<T>, const XY<S>)->XY<decltype(T-S)>;
};
template <typename T, typename S>
auto operator-(const XY<T> z1, const XY<S> z2)->XY<decltype(z1.x - z2.x)>{
decltype(z1.x - z2.x) x;
x = (z1.x - z2.x);
XY<decltype(x)> n(x);
return n;
}
這是你想要的?
template <typename T>
class XY{
private:
T x;
template <typename U, typename S>
friend auto operator-(const XY<U> z1, const XY<S> z2)->XY<decltype(z1.x - z2.x)>;
};
template <typename T, typename S>
auto operator-(const XY<T> z1, const XY<S> z2)->XY<decltype(z1.x - z2.x)>{...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.