如何在 if 语句中使用重载运算符作为条件?

How to use overload operator as condition in a if statment?

这是class

#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;

class Point {
  protected:
    int x, y;
    double operator-(const Point &def){ 
        return sqrt(pow((x-def.x),2.0)+ 
                  pow((y-def.y),2.0));
    }

};

class Circle: public Point {
  private:
    int radius;

  public:
  Circle(){     
    this->x=x;
    this->y=y;
    this->radius=radius;
  }

  Circle(int x, int y, int radius){
this->x=x;
this->y=y;
this->radius=radius;
}
    void printCircleInfo() {
      cout << x << " " << y << " " << radius << " " ;
    }

This is the operator I want to be the condition in my if statement.

bool operator==(const Circle &def){ 
  return (x==def.x) & (y==def.y) & (radius==def.radius);
}
    bool doIBumpIntoAnotherCircle(Circle anotherCircle){
      if (anotherCircle.radius + radius >=   *this - anotherCircle    )
    return true;
      return false;
    }

};

这是主要内容

int main(){
  int x,y,radius;
  const int SIZE = 13;
  Circle myCircleArry[SIZE];
  myCircleArry[0] = Circle(5,3,9);
   cout << endl;
   myCircleArry[0].printCircleInfo(); cout << " ; ";
  ifstream Lab6DataFileHandle;

  Lab6DataFileHandle.open("Lab6Data.txt");
  while (!Lab6DataFileHandle.eof( )) {
 for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>x;
Lab6DataFileHandle>>y;
Lab6DataFileHandle>>radius;
 myCircleArry[i] = Circle(x,y,radius);

 if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
      myCircleArry[i].printCircleInfo(); cout << " ; ";

Here is the If statement

      if ( operator==( Circle &def))
 {cout <<"*";
}


  }
  }
}
  Lab6DataFileHandle.close();

}

How do I use the overloaded operator as the condition of the if statement? If you need any clarification just ask other wise please leave an example in your answer. Thank you for your time.

A == 需要两个参数(即使重载是一个成员),您可以将 if 编写为任何其他 if 语句:

if(circle1 == circle2) { ... }

如果存在匹配的重载,编译器会将其转换为类似以下内容:

if(circle1.operator ==(circle2)) { ... }