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

Back to topics list To post a new topic, please log in or register
avatar
71
dazamate 2016.07.10 00:00
 

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 

 

avatar
2017
gooly 2016.07.10 07:56 #
 
You can't define in mql4 variables as pointers. You can 'create' pointers only by a function call:
double fion(double &d, int &arr[]) {..}

avatar
12983
WHRoeder 2016.07.10 18:11 #
 
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.
avatar
65
jamescater 2016.07.12 15:26 #
 
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.

avatar
12983
WHRoeder 2016.07.12 16:10 #
 
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."
avatar
1109
Ovo 2016.07.13 05:35 #
 
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.
avatar
65
jamescater 2016.07.13 14:48 #
 
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)
}
avatar
12983
WHRoeder 2016.07.13 16:32 #
 
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);

avatar
65
jamescater 2016.07.13 16:41 #
 
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)

avatar
71
dazamate 2016.07.29 03:04 #
 

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? 

avatar
12983
WHRoeder 2016.07.29 03:27 #
 
No. Period. Only a pointer to a class.
Back to topics list  

To add comments, please log in or register