MQL5 The compiler does not distinguish between a class and a pointer to it - page 4

 

that they are equivalent

    a =b;
    b =a;
 
SemenTalonov:

that they are equivalent.

Forget that attribution altogether.

 
Dmitry Fedoseev:

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!

 
SemenTalonov:

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;

 
Vladimir Simakov:

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

m_A[1] = new A();

You cannot save a pointer into an object. The compiler just doesn't notice it.

 
SemenTalonov:

In this example, the second line is not correct

You cannot save a pointer into an object. And the compiler doesn't notice it.

Read my post again.
 
Vladimir Simakov:

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.

 
Vladimir Simakov:

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?

 
SemenTalonov:

This is the 4th interpretation!))

Then why does the output need to call delete? For a temp object?

You cannot call it at all. Pure memory leakage.