小编Alc*_*Alc的帖子

重载运算符上的函数调用不正确

在代码中,为什么是(10 != i)calling==而不是!=?另外两个打电话!=

#include <iostream>

class Integer
{
  int x;

public:
  bool
  operator== (const Integer &i)
  {
    std::cout << "==";
    return x == i.x;
  }
  bool
  operator!= (const Integer &i)
  {
    std::cout << "!=";
    return x != i.x;
  }
  Integer (int t = 0) { x = t; }
};

int
main ()
{
  Integer i;
  std::cout << (i != i) << '\n';   // calls !=
  std::cout << (i != 100) << '\n'; …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading c++20

7
推荐指数
2
解决办法
230
查看次数

标签 统计

c++ ×1

c++20 ×1

operator-overloading ×1