square *s; s = &Candlestick::Levels.squareHolder[i];
- The syntax is not correct. You can't take the address of a variable.
- Only class objects have pointers.GetPointer - Common Functions - MQL4 Reference
- gooly: You can't define in mql4 variables as pointers. You can 'create' pointers only by a function call:Passing by reference is not a pointer.
- gooly: You can't define in mql4 variables as pointers. You can 'create' pointers only by a function call:Passing by reference is not a pointer.
Under the covers passing by reference is identical to passing the address of the object. In fact the concept of a reference is really only relevant from the compilers point of view and just resolves to a "C" pointer address to the object anyway.
The same applies to const correctness and object casts, most are just compiler checks that resolve to no ops in the generated assembler.
Why MQL4 does support taking the address of an object, or at least assigning to an object reference, when it lets you pass an object by reference to a function is completely beyond me.
jamescater: just resolves to a "C" pointer address to the object anyway.
| True, but a MT4 pointer is not an address. "the new operator, which returns a descriptor of the created object." -- Object Pointers - Data Types - Language Basics - MQL4 Reference. Thus my statement "Passing by reference is not a pointer." |
.... Why MQL4 does support taking the address of an object, or at least assigning to an object reference, when it lets you pass an object by reference to a function is completely beyond me.
jamescater: just resolves to a "C" pointer address to the object anyway. | True, but a MT4 pointer is not an address. "the new operator, which returns a descriptor of the created object." -- Object Pointers - Data Types - Language Basics - MQL4 Reference. Thus my statement "Passing by reference is not a pointer." |
Ok, fair enough it's an 8 byte descriptor. But they should still let you declare a local references and assign objects to them in the same way you can pass objects by reference to functions
class TestClass { public: int BarNum; double Value; }; class TestContainer { public: TestClass SubObject; }; void PrintStuff(TestClass& testObj) { Print("BarNum=", testObj.BarNum, " Value=", testObj.Value); } void TestFunction() { TestContainer aContainer; PrintStuff(aContainer.SubObject); // This automatically takes a "reference" to the SubObject when calling the function TestClass& subObj = aContainer.SubObject; // So this should also be possible, (as the function variable and this local variable are both stack based) }
Use a pointer not a reference, they are interchangeable. (Just don't pass a NULL pointer as a reference.) | PrintStuff(aContainer.SubObject); // TestClass& subObj = aContainer.SubObject; // So this should also be possible, TestClass* subObj = GetPointer(aContainer.SubObject); // Works just fine. PrintStuff(subObj); |
Use a pointer not a reference, they are interchangeable. (Just don't pass a NULL pointer as a reference.) |
Thanks, that's very useful.
(I still think they can make the language a bit cleaner in this regard, but this works for now)
OK thanks for providing the info.
I believe what I was trying to do is legal in c++, but I make the mistake of thinking what works in c++ works in MQL4.
I've already coded this a different way, but just out of curiosity and educational purposes, would the right syntax have been..
square *s;
s = GetPointer(Candlestick::Levels.squareHolder[i]);
Or you can't get pointers of variables, period?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey coders
I am just trying to advance my OOP skills here.
I have an array of struct datatype...
When I am iterating through this array of these square structures. I just wanted to see if I could reference each element with a pointer.
as far as I can tell I have the syntax correct. But it will not pass the address of each array element into the pointer.
I am getting these errors...
I have a suspicion that MQL4 will not support this but c++ would.
Kind Regards
Dale