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?
-
LL tmpLL;
What does that line have to do with global variable x? Nothing.
-
if(a == 1) tmpLL = x; tmpLL.price = 2;
Where do you change x? No where.
-
Ask and answered in № 1
-
What does that line have to do with global variable x? Nothing.
-
Where do you change x? No where.
-
Ask and answered in № 1
No. As already answered tmpLL = x Simply copies the value of x to the separate variable tmpLL
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
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.
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
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...
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...
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; }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
in below code,
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?