Because there's no other way to dereference an object. In languages like C or C++ dereferencing is done with the address operator & like so:
void *something = (void *)&obj; // & used as address operator in C/C++, in MQL this results in an illegal operation use error DoSomethingWith(something);
But in MQL the usage of the & operator is strictly associated with call by reference operations like so:
void SetVal(int &key, int val) // & used as reference operator { key = val; }
In MQL it is not allowed to use & as address operator. And so GetPointer is filling this gap.
void *something = GetPointer(obj); // how MQL wants it DoSomethingWith(something);
Because there's no other way to dereference an object. In languages like C or C++ dereferencing is done with the address operator & like so:
But in MQL the usage of the & operator is strictly associated with call by reference operations like so:
In MQL it is not allowed to use & as address operator. And so GetPointer is filling this gap.
Actually you can use it, but only with statically allocated class instances.
#include <trade/trade.mqh> #include <arrays/list.mqh> void OnStart() { CList list; CSymbolInfo eurusd, gbpusd; eurusd.Name("EURUSD"); gbpusd.Name("GBPUSD"); list.Add(&eurusd); list.Add(&gbpusd); CSymbolInfo *ptr = &eurusd; }
The purpose of GetPointer is that it will always return a pointer to an object regardless of whether or not it is statically or dynamically allocated. Think of it like this:
// overloaded template <typename T> T* getPointer(T &obj) { return &obj; } template <typename T> T* getPointer(T *obj) { return obj; }
Actually you can use it, but only with statically allocated class instances.
...That's great to know, especially that you can use a list for this.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I came across the function "get pointer", and I didn't understand why do we need it in mql5.
I know that if I make an object like that:
obj* foo=new obj();
foo is pointer, so if I print/use/return foo I get the pointer to the object of "obj".
Then why do I need the function get pointer?
Thank you!