MQL5 - Unclear about struct variable assignment and change within a function

 

Hi,

in below code, 


struct LL
  {
   double            price;
}
LL x;

int OnInit()
  {
        x.price = 1;
        abc(1);
        
}

void abc(int a) 
  {
   LL tmpLL;
  if(a == 1) tmpLL = x;

  tmpLL.price = 2;
  
}



   

Q1. In above code after calling abc(1) , inside the function abc, is a new object being created for tmpLL variable or tmpLL is just pointing to the same object that x variable is pointing to?
Q2. After abc function is called in OnInit function, will the value of x.price also change to 2?

Q3. If abc(2) had been called instead of abc(1) (within OnInit function) then would tmpLL point to a new struct object that is seperate from x struct object?

 
Varun Maithani:

Q1. In above code after calling abc(1) , inside the function abc, is a new object being created for tmpLL variable or tmpLL is just pointing to the same object that x variable is pointing to?
Q2. After abc function is called in OnInit function, will the value of x.price also change to 2?

Q3. If abc(2) had been called instead of abc(1) (within OnInit function) then would tmpLL point to a new struct object that is seperate from x struct object?

  1.    LL tmpLL;

    What does that line have to do with global variable x? Nothing.

  2.   if(a == 1) tmpLL = x;
      tmpLL.price = 2;

    Where do you change x? No where.

  3. Ask and answered in № 1

 
William Roeder #:
  1. What does that line have to do with global variable x? Nothing.

  2. Where do you change x? No where.

  3. Ask and answered in № 1

@William Roeder
Well i had this concern because if the parameter a is given value 1 then this conditional line is executed : 
tmpLL = x;
 i thought at this point, tmpLL is now pointing to the x object so if one of them changes both of them will.

Is that not  how it works? 
 
Varun Maithani #:
@William Roeder
Well i had this concern because if the parameter a is given value 1 then this conditional line is executed : 
tmpLL = x;
 i thought at this point, tmpLL is now pointing to the x object so if one of them changes both of them will.

Is that not  how it works? 
No.   As already answered tmpLL = x   Simply copies the value of x to the separate variable tmpLL
The are independent of each other
 
Paul Anscombe #:
No.   As already answered tmpLL = x   Simply copies the value of x to the separate variable tmpLL
The are independent of each other

Thanks for confirming.

Now lets say I want it to behave such that they are not independent of each other ... how can i change the code to have that sort of behavior that if 

the parameter a is given value 1 and when this conditional line is executed : 
tmpLL = x;

then tmpLL will be connected to x object and any change in tmpLL will reflect on x object but if a != 1 then tmpLL is an independent object.

I need this behavior somehow but i am not that familiar with MQL5.

Does it have something to do with passing reference of x in parameter perhaps (like shown below)

struct LL
  {
   double            price;
}
LL x;


int OnInit()
  {
        x.price = 1;
        abc(1,x);
        
}

void abc(int a, LL &ll) 
  {
   LL tmpLL;
  if(a == 1) tmpLL = ll;

  tmpLL.price = 2;
  
}

Would this behave in the way i want it?

or perhaps another way or another concept in MQL5 that i am maybe unaware of.

 
Varun Maithani #:

Thanks for confirming.

Now lets say I want it to behave such that they are not independent of each other ... how can i change the code to have that sort of behavior that if 

the parameter a is given value 1 and when this conditional line is executed : 
tmpLL = x;

then tmpLL will be connected to x object and any change in tmpLL will reflect on x object but if a != 1 then tmpLL is an independent object.

I need this behavior somehow but i am not that familiar with MQL5.

Does it have something to do with passing reference of x in parameter perhaps (like shown below)

Would this behave in the way i want it?

or perhaps another way or another concept in MQL5 that i am maybe unaware of.

your logic is flawed-  tmpLL only exists for the duration of the function as it is locally declared.  It cannot be a reference to x and not a reference to x at the same time, it is defined by it's declaration.

You need to be clear about your requirement, but the simplest and best way to learn these things is to try them for yourself and see what results you get...

 
Paul Anscombe #:

your logic is flawed-  tmpLL only exists for the duration of the function as it is locally declared.  It cannot be a reference to x and not a reference to x at the same time, it is defined by it's declaration.

You need to be clear about your requirement, but the simplest and best way to learn these things is to try them for yourself and see what results you get...

Ah ur right i should execute and see what happens first. 
 
I executed and learnt that i would have to assign x to tmpLL after modifying tmpLL to get the effect i need as see below:


struct LL
  {
   double            price;
};
LL x;


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    x.price = 1;
    Print("1. x.price = ",x.price); // Prints 1. x.price = 1.0
        abc(1,x);
    Print("2. x.price = ",x.price); //Prints  2. x.price = 1.0
    abc1(1) ;
    Print("2.2 x.price = ",x.price); //Prints 2.2 x.price = 1.0
    efg();
    
    Print("3. x.price = ",x.price); //Prints  3. x.price = 5.0 
  }
//+------------------------------------------------------------------+
void abc(int a, LL &ll) 
  {
   LL tmpLL;
  if(a == 1) tmpLL = ll;

  tmpLL.price = 2;
  
}

void abc1(int a) 
  {
   LL tmpLL;
  if(a == 1) tmpLL = x;

  tmpLL.price = 3;
  
}


void efg() 
  {
   LL tmpLL;
  

  tmpLL.price = 5;
  x = tmpLL;
  
}