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); }
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 !
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
It should ever return true, but actually, it returns the result of the comparison of two pointers.
What am I doing wrong?