Trying to pass reference a stuct array element to a a pointer

 

Hey coders

I am just trying to advance my OOP skills here. 

I have an array of struct datatype... 

struct square {
   double 
      resistance,
      support;
   int 
      startCandle,
      endCandle; 
   string
      name; 
};

 

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.

for(int i = ArraySize(Candlestick::Levels.squareHolder)-1; i>=0; i--) {
      
      square *s;
      s = &Candlestick::Levels.squareHolder[i];
      
     /* PrintFormat("Resistance Price = %f", s.resistance);
      PrintFormat("Support Price = %f", s.support);
      PrintFormat("Start Candle = %i", s.startCandle);
      PrintFormat("End Candle = %i", s.endCandle);
      PrintFormat("Object Name = %s", s.name);      */
   }

 

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 

 
You can't define in mql4 variables as pointers. You can 'create' pointers only by a function call:
double fion(double &d, int &arr[]) {..}
 
dazamate: as far as I can tell I have the syntax correct.
      square *s;
      s = &Candlestick::Levels.squareHolder[i];
  1. The syntax is not correct. You can't take the address of a variable.
  2. Only class objects have pointers.
    GetPointer - Common Functions - MQL4 Reference
  3. 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.
 
WHRoeder:
  1. 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."
 
jamescater:

.... 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.

From my point of view, 'objects' and 'pointers' (in MQL terminology) are two concurrent concepts, and the former requires passing to a function by reference explicitly, while the latter has it implicitly. With 'pointers' you can do everything, so you may ignore 'objects' in the beginning to avoid confusion.
 
WHRoeder:
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);
 
WHRoeder:
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? 

 
No. Period. Only a pointer to a class.
Reason: