Download MetaTrader 5

[bug] return issue with structs!

To add comments, please log in or register
dck
70
dck  

Hello,

this bug took me some days to catch.
The same code executed from Onstart() works, but from a method or function does NOT.

Here is the Script code:

struct STest{
   double      a, b;
   
   void        STest(): a(0), b(0)                    {}
   void        STest(double A, double B): a(A), b(B)  {}
   void        STest(STest &s): a(s.a), b(s.b)        {}
   
   STest       SelfMod(double c)                      { a+=c; b+=c*.5; return this; }
   void        Log(string src="") const               { Print( "STest:"+ StringFormat("{%G, %G, %s}", NormalizeDouble(a,2), NormalizeDouble(b,2), src)); }
};

class CTest{
   public:
   double a, b;
   
   STest  TestMethod();
};

STest CTest::TestMethod()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from CTest::TestMethod");
   result2.Log("called from CTest::TestMethod");
   
   //method chaining test:
   result.SelfMod(2).Log("called from CTest::TestMethod");
   
   return result;
};

void TestFunction()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from TestFunction");
   result2.Log("called from TestFunction");
   
   //method chaining test:
   result.SelfMod(2).Log("called from TestFunction");
} 

void OnStart()
{
   // from class:
   CTest* c = new CTest();
   STest s = c.TestMethod();
   
   // from TestFunction:
   TestFunction();
   
   // from OnStart:
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from OnStart");
   result2.Log("called from OnStart");
   
   //method chaining test:
   result.SelfMod(2).Log("called from OnStart");
   
   delete(c);
};

The above code Prints:

STest:{5.1, 4.2, called from OnStart}
STest:{3.1, 3.2, called from OnStart}
STest:{3.1, 3.2, called from OnStart}
STest:{0, 0, called from TestFunction}
STest:{0, 0, called from TestFunction}
STest:{3.1, 3.2, called from TestFunction}
STest:{0, 0, called from CTest::TestMethod }
STest:{0, 0, called from CTest::TestMethod }
STest:{3.1, 3.2, called from CTest::TestMethod }

The lines with the red values (0 ,0) are wrong!
In the above code I'm forced to write a standard and copy constructor for the struct, to be able to compile the script, even thought I'm not using them. 

Hope that helps to improve mql5 and that you can fix it.
Daniel

 

Files:
dck
70
dck  

Just added one more test.
The code placed within a struct method isn't working too:

static STest STest::TestMethod()
{
   STest result(1.1, 2.2);
   STest result2 = result.SelfMod(2);
   result.Log("called from STest::TestMethod");
   result2.Log("called from STest::TestMethod");
   
   //method chaining test:
   result.SelfMod(2).Log("called from STest::TestMethod");
   
   return result;
};

the attached file contains the combined tests.

 

Files:
Ilyas
1201
Ilyas  
Thank you for your message. Fixed.
Please try
the new
compiler version.
Files:
MQL5.zip 1063 kb
MQL564.zip 1366 kb
dck
70
dck  

Wow, that was fast, a big thank you!

IT IS WORKING NOW :-) 

To add comments, please log in or register