Download MetaTrader 5

MetaQuote's EA template of using GetPointer(...), bug or a feature?

To add comments, please log in or register
yoda8088 2012.09.15 04:37 


 I am an old time C++ programmer and just started learning MQL5. I notice the pointer concept of MQL5 is different from traditional C++, especially for getting pointer. I read the MQL5 document and quite easy to understand the concept of using function GetPointer(...) to get the pointer of a class object. However, I am getting confused when I see the Expert code from the sample code from Metaquote. The file is Expert.mqh which is used by all automatically generated EAs. Refer to the code highlighted in red, it is trying to get the pointer from a pointer variable. Base on my C++ knowledge, it sounds like getting a pointer's pointer. If that's the case, it is conflict with the function definition of SetSymbol(...) which expects a pointer, rather than pointer's pointer. 

Or it is a MQL5 feature that it treat class and class's pointer as the same way and still work as expected?  

Anyone could suggest it is the right logic here? or a bug of the sample code... Thanks!

in ExpertBase.mqh 

bool CExpert::InitTrade(ulong magic,CExpertTrade* trade=NULL)



   if(m_trade!=NULL) delete m_trade;




      if((m_trade=new CExpertTrade)==NULL) return(false);


   else m_trade=trade;

//--- tune trade object



   //--- set default deviation for trading in adjusted points


//--- ok




In expertBase.mqh 

   CSymbolInfo      *m_symbol;         // pointer to the object-symbol

In ExertTrade.mqh

bool CExpertTrade::SetSymbol(CSymbolInfo* symbol)

Ilyas 2012.09.17 16:46  
function GetPointer for pointer returns this pointer back

class CFoo { };
void OnStart()
   CFoo foo,*pfoo=GetPointer(foo);
of course optimizer of MQL5 compiler removes GetPointer call
To add comments, please log in or register