void*& why the compiler accepts it if it does not work ?

 

the code above erects this help box but 

when something is passed to it it throws an error.

Just curious to what its supposed to do 

void print_type(void*& params[]){
     
     }
int OnInit()
  {
//---
  int h[]={1,2,3};
  print_type();
//---
   return(INIT_SUCCEEDED);
  }
 

No problem.

void print_type(void*& params[])
  {
  }
//---
class A
  {
public:
                     A(void) {}
  };
//---
void OnStart()
  {
   void *ptr[] = {new A};
   print_type(ptr);
  }
 
Alain Verleyen #:

No problem.

void *ptr[]

Could you please tell what such a pointer can be used for?


Edited: Given that pointers are only allowed for classes and functions

void cppExample()
  {
   int var = 63;
   void *voidPtr = &var; // '&' - illegal operation use | 'var' - class type expected
  }

Edited2: together with dynamic_cast?

 
Vladislav Boyko #:

Could you please tell what such a pointer can be used for?


Edited: Given that pointers are only allowed for classes and functions


Edited2: together with dynamic_cast?

MQL5: Added new 'void *' pointers to enable users to create abstract collections of objects. A pointer to an object of any class can be saved to this type of variable.
It is recommended to use the operator dynamic_cast<class name *>(void * pointer) in order to cast back. If conversion is not possible, the result is NULL.
class CFoo { };
class CBar { };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   void *vptr[2];
   vptr[0]=new CFoo();
   vptr[1]=new CBar();
//---
   for(int i=0;i<ArraySize(vptr);i++)
     {
      if(dynamic_cast<CFoo *>(vptr[i])!=NULL)
         Print("CFoo * object at index ",i);
      if(dynamic_cast<CBar *>(vptr[i])!=NULL)
         Print("CBar * object at index ",i);
     }
   CFoo *fptr=vptr[1];  // Will return an error while casting pointers, vptr[1] is not an object of CFoo
  }
//+------------------------------------------------------------------+

Pointer to function is a different topic, I would not mix that with OOP.

As a reminder, a "pointer" in MQL5 are not actual pointer like C++, but a handle to an object

List of changes in MetaTrader 5 Client Terminal builds - New MetaTrader 5 Build 1210: Enhanced Depth of Market and General Improvements
List of changes in MetaTrader 5 Client Terminal builds - New MetaTrader 5 Build 1210: Enhanced Depth of Market and General Improvements
  • 2015.10.29
  • www.mql5.com
New metatrader 5 build 1210: enhanced depth of market and general improvements. An example of how to write ticks to a file and then read them: mql5: modified display of custom indicators with the draw_candles drawing mode
 
Alain Verleyen #:

Pointer to function is a different topic, I would not mix that with OOP.

As a reminder, a "pointer" in MQL5 are not actual pointer like C++, but a handle to an object

Thanks a lot!