声明重载运算符 - 具有 2 种不同类型的对象
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)>{...}
演示
相关文章:
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- C++ 多态模板类,模板类型对象的实例化
- 如何分配适合容纳 T 类型对象的缓冲区(可能过度对齐、可能有运算符 new 等)
- 如何构造一个 std::variant 类型对象,其自身 Templated 和构造函数转发参数
- C++:初始化指向具有不同类型对象的指针数组
- 在类 C++ 中构造的模板类型对象
- 如何为同一类对象的成员函数保留单独的变量副本?
- 如何将颁发者名称设置为x509_req类型对象
- 将任何数据类型/对象作为参数传递以确定其大小
- 如何使用nullptr初始化同一类对象的静态指针数组?
- 将引用类型对象的数组从C#封送到C++
- <T> 从类型对象创建类型<T1>对象的构造函数
- RocksDB是存储同质对象的好选择吗
- C++是否有现有的方法来检查对象是否是派生类型对象
- C 模板:创建与现有对象相同的类型对象
- 链接列表中的虚函数 - 多种返回类型(对象)
- 没有适当操作员()的类型对象的呼叫
- 制作垫子类型对象的数组.输出窗口显示同一帧
- 抽象类型对象的分配
- 在std::list中就地创建自定义类型对象