I had the need to store different objects types (including value types) in one list or array. I struggled to do this in this c++ like language MQL5. How can I do this?
I needed something like this :
Object myObjects[10];
int point = 2;
string symbol = "Test";
myObject[0] = point;
myObject[1] = symbol;
This doesn't seem possible? My next alternative was to create a generic class...I'm afraid I might be hoping for the impossible? I don't see generics?
Luckily I managed to get past my problem but this was only because of my situation I'm in and I believe their might be people that want to do this??
You seem to be looking for a 'Variant' data type like in Visual Basic. This doesn't exist in C++ or MQL5, nor the legacy 'union' data construct from C days. But it would be possible to code up what you're looking for in MQL5 if you write a number of very simple classes derived from CObject, and a generic Object array class derived from CArrayObj.
Ideally operator overloading would make the resultant code as clean as your sample, but MQL5 unfortunately doesn't support it at this stage.
Paul

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I had the need to store different objects types (including value types) in one list or array. I struggled to do this in this c++ like language MQL5. How can I do this?
I needed something like this :
Object myObjects[10];
int point = 2;
string symbol = "Test";
myObject[0] = point;
myObject[1] = symbol;
This doesn't seem possible? My next alternative was to create a generic class...I'm afraid I might be hoping for the impossible? I don't see generics?
Luckily I managed to get past my problem but this was only because of my situation I'm in and I believe their might be people that want to do this??