Errors, bugs, questions - page 453

 
Interesting:

Don't create dynamic objects - you won't have to use now and everything else related to these objects (but then you won't be able to do a lot of things).

You want to understand not the meaning of now, but the meaning of working with dynamic objects...

//////////////////////////////////////// FOLLOW MY TEXT (I CAN'T GET OUT OF THE QUOTE) :) //////////////////////////////////////////////////////////////

void OnStart()
{
//---
primer obekt;
obekt.f1();
}
//+------------------------------------------------------------------+
class primer
{
public:
void f1();

primer();
};

primer::primer()
{
Alert("I think the initialisation here too is only at the time of creation");
};

primer::f1()
{
int l[];
ArrayResize(l,3);
l[0]=87;
l[1]=67;
l[2]=57;
ArrayResize(l,5);
l[3]=47;
l[4]=37;
Alert(l[0]," ",l[1]," ",l[2]," ",l[3]," ",l[4]);
};

This code does not produce an error and the array is dynamic. Why????

And if you want to destroy it:

{

obekt;
obekt.f1();


}

It seems that they took C++ and messed up address handling and put it into MQL

 

220Volt:

FURTHER IS MY TEXT (I CAN'T GET OUT OF QUOTE) :)

This code does not give an error and the array is dynamic. Why????

1. Why should it be executed with an error?

a) The object is created automatically (this is caused by the method of declaring an object instance in OnStart) and is automatically deleted

primer  obekt1;   //Конструктор будет вызван автоматически уже на этой строчке
 primer *obekt2; //Указатель на динамический объект

void OnStart()
{
obekt2 = new primer; //Конструктор выполняется вот тут 

obekt.f1();

delete(obekt2); //Тут выполняется деструктор (отсутствие delete приведет к утечки памяти в момент завершения работы скрипта)
}

As for objects, their instances can be created automatically or dynamically. In the first case, the constructor is usually executed during initialization, in the second case it is executed after new operator is used.

b) Logically, there should be no errors in f1() either (although I personally would have implemented it differently).

As for dynamic arrays. They differ from static arrays in that such arrays do not have known dimensionality, which means that before you work with such an array, you should specify it using ArrayResize (you should specify a new size in the first array dimension). It is important to note that array elements are numbered from 0.

2. If you are interested in working with dynamic objects, please carefully study this example - Tetris (although I still cannot understand what it does in the old forum)...

 

I'm sorry to intrude, but I just really want to get to the bottom of this. I'll tell you what I'm wrong about.

If we look at the C++ notation (we have a declared primer class)

primer *ukazatel=new primer;

says that ukazatel contains the address in memory where our object, a copy of the primer class, is stored. From MQL addresses are removed, and ukazatel contains an object descriptor. What do we get out of it? The only difference I noticed, if inside the block we declared via new object and forgot to use delete before finishing the block, then the object will be deleted only when the program will be finished. And after exit from the block connection with this object via handle will be lost.

I had the thought that if declared through new, then the memory for the object is allocated dynamically (ie, it can increase its size), and if no new, then the object can only have static variables. But my assumption was not confirmed (the script, which I posted earlier, the object is declared not through new, but it deals with a dynamic variable).

And after all these passions I'm in a not very nice situation, and I don't understand what's the difference with new or without it, and if the compiler itself fixes something there, why should I bother?

Документация по MQL5: Основы языка / Переменные / Статические переменные
Документация по MQL5: Основы языка / Переменные / Статические переменные
  • www.mql5.com
Основы языка / Переменные / Статические переменные - Документация по MQL5
 

Even tried to do something like what is described in the documentation (to find out something about new) :

switch(5)

{

case 5: m_shape=new CTetrisShape1; // this option doesn't work at all (and this is from the documentation!!!!!!) only this way

( m_shape=new CTetrisShape1; )

}

and now we cannot access the m_shape.___ object, an error will be displayed

 

220Volt:

And now I find myself in not a very good situation after all these passions, and I don't understand what's the difference with new or without it, and if the compiler fixes something there, why should I bother at all?

Object instances are created dynamically using the new if operator (which is why I asked to see the Tetris example):

1. The number of object instances is not known in advance (but it is assumed that there will always be more than one);

2. If one wants to create an array of pointers to "heterogeneous" objects, which have the same ancestor;

3. if it's supposed that the object (or rather a pointer to this object) will be used as a parameter for a procedure or function (you should probably pass it to the library);

4. If it is intended to work with one object (read: a pointer to this object) in different parts of the program. For example, the same object "order" can be in: an array of orders created by the Expert Advisor; an array of orders for a certain symbol; an array of orders that formed a certain position, and so on.

5. There are many more tasks which are solved by applying dynamically created vows and pointers to them.

 
220Volt:

Even tried to do something like what is described in the documentation (to find out something about new) :

........................

and now we cannot access the m_shape.___ object, an error will be thrown

Actually in the Tetris example it is implemented in the following way (notice that in your case the break operator is missing)

   switch(nshape)
     {
      case 0: m_shape=new CTetrisShape1; break;
      case 1: m_shape=new CTetrisShape2; break;
      case 2: m_shape=new CTetrisShape3; break;
      case 3: m_shape=new CTetrisShape4; break;
      case 4: m_shape=new CTetrisShape5; break;
      case 5: m_shape=new CTetrisShape6; break;
      case 6: m_shape=new CTetrisShape7; break;
     }

Don't forget to check if the pointer is NULL

 if(m_shape!=NULL)
 {
//С указателем можно работать
 }
 

Interesting thank you for your help
 

Developers.

In documentation please specify "explicitly" all things that either do not work in tester, or work with certain features (so that unnecessary questions do not arise).

For example, you should explicitly mention the peculiarities of local time and GMT time (equated to server time), as well as the problematic nature of TimeGMTOffset to get the expected result.

 

Who knows -- is there a normal way to translate a Unicode array of characters into a string?

 
TheXpert:

Who knows -- is there a normal way to translate a Unicode array of characters into a string?

It seems to me that I would have to translate each element of the array individually.
Документация по MQL5: Основы языка / Переменные
Документация по MQL5: Основы языка / Переменные
  • www.mql5.com
Основы языка / Переменные - Документация по MQL5
Reason: