[英]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.