Can't figure out how to overload the binary operator== in my class

 

Hello,

   here https://www.mql5.com/en/docs/basis/function/operationoverload explain how to overload operators in MQL5.

   For example with the first example (suppose I use a class instead a struct) in the link I try:

class complex
  {
   double            re; // Real part
   double            im; // Imaginary part
   
   complex(double r, double i);
   bool operator==(complex *p1) {return true;};

   }
void complex(double r, double i):re(r),im(i) {};
bool complex::operator==(complex *p1) {return true;};

  It should ever return true, but actually, it returns the result of the comparison of two pointers.

complex *p1 = new complex(3,4);
complex *p2 = new complex(3,4);

Print(p1 == p2); // returns false

What am I doing wrong?

Documentation on MQL5: Language Basics / Functions / Operation Overloading
Documentation on MQL5: Language Basics / Functions / Operation Overloading
  • www.mql5.com
Operation overloading allows the use of the operating notation (written in the form of simple expressions) for complex objects - structures and classes. Writing expressions using overloaded operations simplifies the view of the source code, because a more complex implementation is hidden. For example, consider complex numbers, which consist of...
 
framontb:   It should ever return true, but actually, it returns the result of the comparison of two pointers.
It should not require a pointer but a reference to complex and compare the two parts values.
 

Try it this way. 

class Complex {
public:
   double            re; // Real part
   double            im; // Imaginary part
                     Complex(double r, double i);
   bool              operator==(Complex &other);
};
//+------------------------------------------------------------------+
void Complex::Complex(double r, double i):re(r),im(i) {};
bool Complex::operator==(Complex &other) {
   return (this.re == other.re && this.im == other.im);
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart() {
//---
   Complex v1(3, 5), v2(3, 5), v3(3, 4);
   Print(v1 == v2);
   Print(v2 == v3);
}
 
nicholi shen:

Try it this way. 

Thanks for your help. 

What I'm trying to do is compare two pointers of the object Complex, but in my own way (looking inside the object the pointer aims at). I'm not trying to compare two Complex objects itself.

It could looks like rather weird but... is what it is !

 
William Roeder:
It should not require a pointer but a reference to complex and compare the two parts values.

Yes, it will work.

But what if what I want to compare are pointersto these objects, overloading the operator==?

I mean, are there a way to overload the operator== when it comes to pointers of specific classes? (not if the pointers itself are the same, but to look inside and make decisions).

A workaround is to create objects of the class and to cast the pointers to them, but this implies instantiate new objects, what I don't want to do. 

Reason: