Features of the mql5 language, subtleties and tricks - page 48

 
Rashid Umarov:

That is - you need to use pointers to functions

Not necessarily. Classes with an overridden operator () are also great for this
 
Combinator:
Not necessarily. Classes with an overridden operator () are also great for this

Is it possible to overload this operator in MQL?

 

Actually, the most versatile and flexible variant is, of course, pointers to functions. You need to be able to sort by different parameters, and there is only one class method. At the moment there are no delegates in MQL, so we have pointers to static methods of the class. It looks like this:

struct DrawData
{
  typedef int(*CompareDrawData)(const DrawData& obj1, const DrawData& obj2); 
  
  static int ComparePrice(const DrawData& obj1, const DrawData& obj2) { return obj1.price > obj2.price ? 1 : obj1.price < obj2.price ? -1 : 0; } 
  static int ComparePercent(const DrawData& obj1, const DrawData& obj2) { return obj1.percent > obj2.percent ? 1 : obj1.percent < obj2.percent ? -1 : 0; } 
  
  float price;
  float percent;
};


template<typename T1, typename T2>
void Sort(T1& array[], T2 comparer)
{
  //.......
}


void Main()
{
   DrawData items[];
   // filling
   Sort(items, (CompareDrawData)DrawData::ComparePrice);
}
 
Alexey Navoykov:

Can this operator be overloaded in MQL?

I don't think so
 

Thank you all who responded. I'll try and figure it out.

 
Comments not related to this topic were moved to"Questions from Beginners in MQL5 MT5 MetaTrader 5".
 

If you press TAB immediately after the words if, else, while, for, do, there will be a little extra construction...

 
If debugging is done on the history, the values of input parameters are taken not from the source, but from the Tester Parameters tab.
 
fxsaber:

If you press TAB right after the words if, else, while, for, do, there will be a little extra construction...

And not only after them. Also after OnInit, OnTick, OnCalculate, OnDeinit, class, etc. And it would be very nice if you could set the template of these constructions yourself.
 
During optimization, the OnTester is always executed, even if the Custom criterion is not selected.
Reason: