MQL5 The compiler does not distinguish between a class and a pointer to it - page 4
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
that they are equivalent
that they are equivalent.
Forget that attribution altogether.
Forget about this assignment altogether.
So it's not me who should forget, it's the compiler that shouldn't allow you to write it that way.
And at the moment it not only compiles but runs without errors!
So it's not me who needs to forget, it's the compiler that shouldn' t allow it to be written that way.
And at the moment it not only compiles but runs without errors!
The compiler allows it because you can, if you know what you're doing.
class A { public: int iValue; A(){Print("++");} ~A(){Print("--");} }; //...................... A m_A[2]; void OnStart() { A a; m_A[0] =a; m_A[1] = new A(); }
Run and see how many times and when the constructor and destructor are called.
m_A[0]=a; in this case it is equal to: m_A[0].iValue=a.iValue;
new A(), in this case, creates a new object, let's call it temp and the following happens: m_A[1].iValue=temp.iValue;
Run and see how many times and when the constructor and destructor are called.
m_A[0]=a; in this case it is equal to: m_A[0].iValue=a.iValue;
The second line in this example is incorrect
You cannot save a pointer into an object. The compiler just doesn't notice it.
In this example, the second line is not correct
You cannot save a pointer into an object. And the compiler doesn't notice it.
Run and see how many times and when the constructor and destructor are called.
m_A[0]=a; in this case it is equal to: m_A[0].iValue=a.iValue;
new A(), in this case, creates a new object, let's call it temp and the following happens: m_A[1].iValue=temp.iValue;
Probably constructor 3, destructor 2. The rest is correct.
and the following happens: m_A[1].iValue=temp.iValue;
This is the 4th interpretation!))
Then why is it necessary to call delete in the output? For a temp object?
This is the 4th interpretation!))
Then why does the output need to call delete? For a temp object?