How to use virtual compare method from CObject

 

Dear All,

I tried to use the virtual Compare method from CObject Class, but can't manage to make it works, any insights?

 Regards,

//+------------------------------------------------------------------+
//|                                           och_test_SortArray.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql4.com"
#property version   "1.00"
#property strict


#include <Object.mqh>
#include <Arrays\ArrayObj.mqh>

class CStrategy : public CObject
{
  protected:
  int                 m_id;
  
  public:
  void                CStrategy(){m_id = MathRand();};
  void               ~CStrategy(){};
  int                 StrategyID(){return(m_id);};
  int                 Type(void) const { return(1);};
  int                 Compare(CStrategy *node,const int mode=0);
};

int CStrategy::Compare(CStrategy *node,const int mode=0)
{
  Print(__FUNCSIG__ + ":: Here we are!");
  return(m_id > node.StrategyID());
}


class CAdaptive_Strategy : public CStrategy
{
  public:
  int                 Type(void) const { return(2);};
  
};

CArrayObj            *strategies;  
CStrategy            *strategy;
CAdaptive_Strategy   *adaptive_strategy; 
CObject              *object;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   MathSrand(0);
   
   strategies = new CArrayObj;
   for (int i=0;i<10;i++){
      adaptive_strategy = new CAdaptive_Strategy;
      //Print(__FUNCSIG__ + " :: i = " + IntegerToString(i) + " id = " + IntegerToString(adaptive_strategy.StrategyID()) + " i = " + IntegerToString(i));
      strategies.Add(adaptive_strategy);
   }
      
   strategies.Sort();
   
   Print(__FUNCSIG__ + " :: Nb strategies = " + IntegerToString(strategies.Total()) + " IsSorted = " + string(strategies.IsSorted()));
   
   for (int i=0;i<strategies.Total();i++){
    adaptive_strategy = strategies.At(i);
    if (adaptive_strategy.Type() == 2) // be carrefull : BUG need to test a property before access to some members...
    //Print(__FUNCSIG__ + ":: adaptive_strategy.type = " + adaptive_strategy.Type() + " id = " + IntegerToString(adaptive_strategy.StrategyID()));
    Print(__FUNCSIG__ + ":: i = " + IntegerToString(i) + " id = " + IntegerToString(adaptive_strategy.StrategyID()));
  }
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
 
}
//+------------------------------------------------------------------+
 

I assume you defined a new Compare method

instead of using the defined parameters

and cast to your class later inside the method.

 
ugo58:

I assume you defined a new Compare method

instead of using the defined parameters

and cast to your class later inside the method.

Hi ugo58,
thanks for your reply, but I can't catch your point? 
What do you mean exactly? 
The result of the expert above should be a list with ascending strategyID value. This is the result I expect to obtain.
Regards,
 
och:
Hi ugo58,
thanks for your reply, but I can't catch your point? 
What do you mean exactly? 
The result of the expert above should be a list with ascending strategyID value. This is the result I expect to obtain.
Regards,
int CStrategy::Compare(const CObject *node,const int mode=0) // instead of Compare(CStrategy *node,const int mode=0)
{
  // Print(__FUNCSIG__ + ":: Here we are! ",this.m_id," ",((CStrategy*)node).StrategyID());
  if (this.m_id > ((CStrategy*)node).StrategyID()) return(1);
  if (this.m_id < ((CStrategy*)node).StrategyID()) return(-1);
  return(0);
}
Reason: