PLO. Application issues - page 7

 
equivalent23:

Of course:

I realized that the problem is how tocorrectly fill the shapes[10]array with instances of classes derived from CShape. The option I suggested doesn't work for some reason. Let's think.
 
Yedelkin:
I realized that the problem is how tocorrectly fill the array shapes[10] with instances of classes derived from CShape. My suggested variant does not work for some reason. Let's think.

The array must be of class pointer type, not of class type itself.

Then we can apply new operator.

Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
Документация по MQL5: Основы языка / Операторы / Оператор создания объекта new
  • www.mql5.com
Основы языка / Операторы / Оператор создания объекта new - Документация по MQL5
 
AlexSTAL:

The array must be of the class pointers type, not of the class itself.

Then you can apply the new operator.

Can I give you an example?

It's just that the example I gave was taken from documentation and it's not clear how it should work...

 
Yedelkin:
I realized that the problem is how tocorrectly fill array shapes[10] with instances of classes derived from CShape. My suggested variant doesn't work for some reason. Let's think.

Think or not, 100 roubles is not money.

The example is 100-percent glitchy, and such an implementation is either not well thought out or not finished.

I won't cite the results of my own experiments in code, I will only give you the conclusions:

1. If you declare objects not as classes but as structures, they will easily fit into an array. But the viriality will have to be removed, and you can only work with the functionality of the type of objects that the array has (in this case, a base class);

2. 2. You can declare them as pointers, and then you can form an array:

a) if pointers are not cleared, memory leaks occur;

b) you also can't work normally with descendant functionality.

PS

To be more exact - functionality which was declared in base class is available and also visible in descendants, but everything that appeared in descendants is inaccessible.

Документация по MQL5: Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов
Документация по MQL5: Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов
  • www.mql5.com
Стандартные константы, перечисления и структуры / Константы объектов / Свойства объектов - Документация по MQL5
 
AlexSTAL:

An array must be of type pointers to classes, not of type of class itself.

Then you can apply the new operator.

Yes, I also thought in this direction, about using pointers. But then the conditions from the documentation are not fulfilled:

"For a square (class CSquare) the area is calculated through the sides, for a circle (class CCircle) the area is expressed through the radius and so on. We can create an array to store objects of type CShape, in which we can store both the object of a base class and all its descendants. Later we can call the same function for any element of this array.

...Since we need the corresponding values of m_radius and m_square_side members to calculate the area of a square and a circle, we have added the SetRadius and SetSide() functions to the declaration of the corresponding class. Now we can declare an array of type Shape and fill it with objects of derived classes..."

 
AlexSTAL:

An array must be of type pointers to classes, not of type of class itself.

Then you can apply the new operator.

It won't work normally. Although it will form an array.

Yedelkin:

...Since calculating the area of a square and a circle requires corresponding values of m_radius and m_square_side members, we have added SetRadius and SetSide() functions in the declaration of the corresponding class. Now we can declare an array of Shape type and fill it with objects of derived classes..."

The whole thing is that if the array is of base class type, the descendant functionality cannot be accessed. i.e. SetRadius() and SetSide() become unavailable.

That's why I can see three options:

1. the array should be homogeneous (if possible);

2. I don't understand something about OOP philosophy and architecture of MQL5;

3. Either the developers failed to implement the example, or they didn't follow through with the question.

 
Interesting:
It won't work properly. It will form an array, though.
What do you mean it won't work?
 
Interesting:

To be more exact - functionality which was declared in base class is available, and at the same time it is visible at descendants, but everything that appeared at descendants is inaccessible.

Yup, that's exactly what I came across.

Interesting:

The example is 100 per cent glitchy ...

It turns out that we need an answer from the Handbook authors on this topic.

 
Interesting:
It won't work properly. It will form an array, though.

The trick is that if the array is of base class type, the descendant functionality cannot be accessed. i.e. SetRadius() and SetSide() become unavailable.

That's why I can see three options:

1. the array should be homogeneous (if possible);

2. I don't understand something about OOP philosophy and architecture of MQL5;

3. the developers have either betrayed an example that doesn't work, or they haven't completed the task.

Execute:

class CBase
  {
public:
   void m_radius() {Print("CBase");}
  };

class CTest : public CBase
  {
public:
   void m_radius() {Print("CTest");}
  };

CBase* Base;

void OnStart()
  {
   Base = new CTest;
   Base.m_radius();
   ((CTest *)Base).m_radius();
   delete Base;
  }

it could be done in a much simpler way... by virtualization:

class CBase
  {
public:
   virtual void m_radius() {Print("CBase");}
  };

class CTest : public CBase
  {
public:
   virtual void m_radius() {Print("CTest");}
  };

CBase* Base;

void OnStart()
  {
   Base = new CTest;
   Base.m_radius();
   delete Base;
  }
 
AlexSTAL:
What do you mean it won't work normally?

1. The descendant functionality is not available. I don't know who or how, but personally I never got access to SetRadius() and SetSide() from the Array.Maybe there's a way to solve the problem with autogenous, but I want to do without it.

2. Maybe I'm working with pointers wrong, but I either have a leak all the time or have to hit the pointers right in the block where the main work is done.