void*& why the compiler accepts it if it does not work ?
No problem.
void print_type(void*& params[]) { } //--- class A { public: A(void) {} }; //--- void OnStart() { void *ptr[] = {new A}; print_type(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?
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?
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
- 2015.10.29
- www.mql5.com
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