标签: operator-overloading

如何在 C++ Gtest 中测试输入和输出重载运算符

我正在使用这里的以下示例

考虑我有以下课程

#include <iostream>

class Distance {
private:
  int feet;             
  int inches;           

public:
  Distance()             : feet(), inches() {}
  Distance(int f, int i) : feet(f), inches(i) {}

  friend std::ostream &operator<<( std::ostream &output, const Distance &D )
  { 
     output << "F : " << D.feet << " I : " << D.inches;
     return output;            
  }

  friend std::istream &operator>>( std::istream  &input, Distance &D )
  { 
     input >> D.feet >> D.inches;
     return input;            
  }
};
Run Code Online (Sandbox Code Playgroud)

我正在使用 Gtest 来测试这个类。

但我找不到更好的方法来测试它。

我可以使用 gtest 中提供的宏ASSERT_NO_THROW …

c++ operator-overloading googletest

0
推荐指数
1
解决办法
4580
查看次数

重载大于有或没有朋友的运算符

假设我有以下课程:

class Point{
private:
int x,y;

public:
int get_x() const {return x;}
int get_y() const {return y;}

Point() :x(0),y(0){}
Point(int x,int y):x(x),y(y){}
Point(const Point& P){
    x = P.get_x();
    y = P.get_y();
}
Point& operator=   (const Point& P) {
            x = P.get_x();
            y = P.get_y();

    return *this;
}
friend ostream& operator<<(ostream& os,const Point& P) {

    os<<"["<<P.get_x()<<", "<<P.get_y()<<"]";
    return os;
}


Point operator - (const Point &P){
    return Point(x-P.get_x(),y-P.get_y());
}

friend bool operator > (const Point &A, const Point &B) {
    return …
Run Code Online (Sandbox Code Playgroud)

c++ overloading operator-overloading

0
推荐指数
1
解决办法
2792
查看次数

Kotlin:带有 get/set 运算符的具体化泛型的 UnsupportedOperationException

我正在尝试将库中的函数read和分别转换为运算符函数和。我的函数使用具体化的泛型类型,它们也是内联方法。(你可以在这里看到我的图书馆writegetset。)

编译器没有问题让我通过,我不明白为什么使用非运算符函数推理工作得很好,但使用运算符在运行时失败。

Exception in thread "main" java.lang.UnsupportedOperationException: This function has a reified type parameter and thus can only be inlined at compilation time, not called directly.
    at kotlin.jvm.internal.Intrinsics.throwUndefinedForReified(Intrinsics.java:173)
    at kotlin.jvm.internal.Intrinsics.throwUndefinedForReified(Intrinsics.java:167)
    at kotlin.jvm.internal.Intrinsics.reifyJavaClass(Intrinsics.java:201)
Run Code Online (Sandbox Code Playgroud)

代码如下:

operator inline fun <reified T> get(address: Long): T {
    ...
}

operator inline fun <reified T> get(address: Int): T = get(address.toLong())

operator inline fun <reified T> set(address: Long, data: T) {
   ...
}

operator inline fun <reified T> set(address: Int, …
Run Code Online (Sandbox Code Playgroud)

function operator-overloading jvm-languages reification kotlin

0
推荐指数
1
解决办法
2769
查看次数

C# 重载泛型 + 运算符

我试图通过在列表中添加一个项目来添加一个列表。

我试图实现的代码的目标使用:

List<int> numbers = new List<int>();
numbers += 10;
Run Code Online (Sandbox Code Playgroud)

我尝试过的。“operator +”应该重载 +,“this IList”应该扩展通用 IList。

public static IList<T> operator +<T>(this IList<T> list, T element)
{
    list.Add(element);
    return list;
}
Run Code Online (Sandbox Code Playgroud)

然而它不起作用,在 Visual Studio 2012 中到处都是红色下划线。我做错了什么?这不可能吗?为什么这适用于标准类但不适用于通用类?

c# generics operator-overloading

0
推荐指数
1
解决办法
1029
查看次数

Swift 3 协议重载运算符

我有一个声明 type 属性的协议Int。我也有几个符合该要求的类Protocol,现在我需要+为所有这些类重载运算符。由于运算符+将根据声明的属性工作,因此我不想在每个类中单独实现该运算符。

所以我有

protocol MyProtocol {
    var property: Int { get }
}
Run Code Online (Sandbox Code Playgroud)

我想要有类似的东西

extension MyProtocol {
    static func +(left: MyProtocol, right: MyProtocol) -> MyProtocol {
        // create and apply operations and return result
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上我成功地做到了这一点,但尝试使用它时出现错误ambiguous reference to member '+'

当我将运算符重载函数分别移动到每个类时,问题消失了,但我仍在寻找一种解决方案以使其与协议一起使用。

xcode operator-overloading ios swift

0
推荐指数
1
解决办法
977
查看次数

有没有办法强制对对象进行Python字符串连接操作?

我对拥有一个类和该类的实例并将它们连接起来有一些疑问。通常是:

class MyClass(object):
    # __init__, more code and so on...
    def __str__(self):
        return "a wonderful instance from a wonderful class"

myInstance = MyClass()
message = "My instance is " + str(myInstance) + "."
print(message)
Run Code Online (Sandbox Code Playgroud)

正如我在查看 python 文档时记得的那样,这将转到__str__()in 的方法并成功打印该行。MyClass

但是,不可能通过某些运算符重载来实现这一点吗?:

message = "My instance is " + myInstance + "."
Run Code Online (Sandbox Code Playgroud)

我只是很好奇,因为我认为这是可能的,但我在 python 文档中找不到这个。在这种情况下,我有一个对象,并且认为我可以做得更短,并且还在类层次结构的根中实现运算符重载,从而保存子级中的写入。

我想我无法解决这个str()问题。我可以吗?

python string operator-overloading python-3.x

0
推荐指数
1
解决办法
580
查看次数

在继承的模板类中使用下标[]运算符

我有一个模板类Array<T>,定义了以下三个成员函数。

\n\n
template <typename T>\nconst T& Array<T>::GetElement(int index) const {\n    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);\n    return m_data[index];\n}\n\ntemplate <typename T>\nT& Array<T>::operator [] (int index) {\n    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);\n    return m_data[index];\n}\n\ntemplate <typename T>\nconst T& Array<T>::operator [] (int index) const {\n    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);\n    return m_data[index];\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

接下来我有另一个NumericArray<T>继承自 的模板类Array<T>。此类包含一个重载运算符+

\n\n
template <typename T>\nNumericArray<T> NumericArray<T>::operator + (const NumericArray<T> &na) const {\n    unsigned int rhs_size = this -> Size(), lhs_size = na.Size();\n    if(rhs_size != lhs_size) throw SizeMismatchException(rhs_size, lhs_size);\n\n    NumericArray<T> …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates operator-overloading

0
推荐指数
1
解决办法
362
查看次数

cpp运算符重载操作数位置[] vs ==

与有何char& operator[]不同bool operator==?一种将操作数放在中间[],另一种将操作数放在后面==。它如何知道将操作数放在哪里?左括号和右括号有什么特殊的技巧吗?

c++ operator-overloading

0
推荐指数
1
解决办法
64
查看次数

如何让重载的 == 运算符与 LINQ 和 EF Core 一起使用?

基本上,我有一个使用 EF Core 的项目。为了在比较两个对象(协议类)是否相等时缩短 lambda,我重写了 Equals 方法并重载了 == 和 != 运算符。然而,LINQ似乎并不关心它,仍然使用引用来确定相等性。谢谢

正如我之前所说,我重写了 Equals 方法并重载了 == 和 != 运算符。没有运气。我还尝试过实现 IEquatable 接口。也没有运气。

我正在使用:EF Core 2.2.4

//协议类

[Key]
public int ProtocolId {get;set;}
public string Repnr {get;set;}
public string Service {get;set;}

public override bool Equals(object obj)
{
    if (obj is Protocol other)
    {
        return this.Equals(other);
    }
    return false;
}

public override int GetHashCode()
{
    return $"{Repnr}-{Service}".GetHashCode();
}

public bool Equals(Protocol other)
{
    return this?.Repnr == other?.Repnr && this?.Service == other?.Service;
}

public static bool …
Run Code Online (Sandbox Code Playgroud)

c# linq equals operator-overloading entity-framework-core

0
推荐指数
1
解决办法
995
查看次数

Swift - 如何重载 Int 的 += 以在 nextPage(page: pageIndex += 1) 等函数调用中将运算符作为参数传递?

我想知道如何执行以下操作:-

    // This is not correct
    func += (inout lhs: Int, rhs: Int) -> Int {
        return lhs + rhs
    }


Objective Usage:- scrollTo(pageIndex: pageIndex += 1)

Run Code Online (Sandbox Code Playgroud)

math operator-overloading ios swift inout

0
推荐指数
1
解决办法
684
查看次数