- Please edit your (original) post and use the CODE
button (Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor -
things[i].Make(i, i*i);
This doesn't do what you think it does.T operator[](uint index) { return m_data[index]; }
Operator[] makes a copy and returns it. You then update the copy and then throw it away. You would know this had you simply stepped into the code with the debugger. - You can't return a pointer, because it's a struct not a class.
- If it was a class and you returned a pointer, then the Make would be updating the original.
Thank you. I didn't know the difference between struct and class in MQL4. I thought pointers are not available in MQL.
Mikołaj Gogola: I thought pointers are not available in
MQL.
Correct, only handles to a class. They act like references. A lot has changed since Build 600 and Higher. 2014.02.03
Is any graceful way to make my vector class work on basic types like double or int?
I could do wrapper class around basic types
How can I make it work?
I could do wrapper class around basic types
class Double { public: double value; };but there is still need to get underlying basic type with dot operator
vector <Double> myVector; myVector[4].value = 7.5;and I can't create macro to hide it, because macro has to have the same name as the struct and it is not allowed.
How can I make it work?
Mikołaj Gogola: Is any graceful way to make my vector class work
on basic types like double or int?
No, because MTx doesn't have pointers. Your operator[] can't return a pointer, so you can not write v[i]=v.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
```
template < typename T >
struct vector {
vector() {}
vector(int arraySize) {
if (arraySize < 0) { arraySize *= -1; }
ArrayResize(m_data, arraySize);
}
vector(vector & rhs) {
if (size() < rhs.size()) { ArrayResize(m_data, rhs.size()); }
for(uint i = 0; i < size(); ++i) { m_data[i] = rhs.m_data[i]; }
}
vector operator=(vector & rhs) {
if (size() < rhs.size()) { ArrayResize(m_data, rhs.size()); }
for(uint i = 0; i < size(); ++i) { m_data[i] = rhs.m_data[i]; }
return this;
}
T operator[](uint index) { return m_data[index]; }
void push_back( T & value ) {
ArrayResize(m_data, ArraySize(m_data) + 1);
m_data[ ArraySize(m_data) - 1 ] = value;
}
uint size() { return ArraySize(m_data); }
void resize(uint newSize) { ArrayResize(m_data, newSize); }
void erase() {
ZeroMemory(m_data);
ArrayResize(m_data, 0);
}
void assign(uint index, T & value) {
m_data[index] = value;
}
T last() { return m_data[ ArraySize(m_data) - 1 ]; }
private:
T m_data[];
};
//program
struct Something {
Something() : x(0.0), y(0.0) {}
void Make(double a, double b) { x = a; y = b; }
double Get() { return x+y; }
double x,y;
};
vector<Something> things;
void OnStart()
{
for(int i = 0; i < 10; ++i) {
things.push_back( Something() );
}
for(uint i = 0; i < things.size(); ++i) {
things[i].Make(i, i*i);
}
string s;
for(uint i = 0; i < things.size(); ++i) {
s += (string)things[i].Get() + "\n";
}
Alert ( s );
}
```
This script is printing 0 for each element instead of i+i*i sum.