PLO. Application issues - page 6

 
Interesting:

Even assuming void Del(C_A *p) in the ancestor is sufficient to delete the pointer of any descendant, I see no point in using

Well, this is another question :) - the question of expediency. What I am interested in is the question of correctness/literacy of such an approach.
 
Yedelkin:
That's another question :) - the question of expediency. I am interested in question of correctness/literacy of such approach.

All options seem to be good, as long as the pointer is removed correctly.

And it seems to be removed, even memory leaks stopped appearing (I don't know why it appeared last time)...

 
Interesting:

All options seem to be good as long as the pointer is deleted correctly.

And it seems to be removed, even memory leaks stop appearing (who knows why it came up last time)...

It looks cool from outside - object crashes itself.
 
Yedelkin:
It looks cool from the outside - the object will crash itself.

It's not cool - when you leave, you "kill" everyone...

This code is a bit cooler (crashes everything in the array).

But the coolest part is when the array is built into a child class...

//----------------------------------------------------------------------------//
//Work variables
C_A *ArrPointer[4];

int Result = 0; //Returned importance
//----------------------------------------------------------------------------//

ArrPointer[0] = new C_A;
ArrPointer[1] = new C_B;
ArrPointer[2] = new C_C;
ArrPointer[3] = new C_D;

  for(int i=0;i<4;i++)
  {
  ArrPointer[i].Free(ArrPointer[i]);
  Print("CheckPointer №",i+1," - ",CheckPointer(ArrPointer[i]));
  }
//----------------------------------------------------------------------------//
 

Polymorphism question.

There is an example of this in the documentation:

CShape[10] shapes;                       // массив объектов CShape
//... здесь заполняем массив производными объектами
for  (int i=0; i<10;i++)  
  {
   //--- тип и площадь фигуры
   Print("Объект типа "+shapes[i].GetType()+" имеет площадь "+ shapes[i].GetArea());
  };


Not quite sure what should be in place

//... здесь заполняем массив производными объектами

Can I have at least 1 example?

And why is the size of an array specified for a variable type and not for the variable itself?

In addition, there seem to be errors in declaring classes in the same chapter of the documentation:

class CShape{};

class CCircle{} : public CShape

class CSquare{} : public CShape

 
equivalent23:

And why is the dimensionality of the array specified by the variable type and not by the variable itself?

Is that what you mean?
CShape[10] shapes;                       // массив объектов CShape

This is a pure typo.

23:

Besides, there seem to be errors in declaration of classes in the same documentation chapter:

class CShape{};

class CCircle{} : public CShape

class CSquare{} : public CShape

Are you referring to curly brackets after the name of the class being declared?

 
equivalent23:

Question about polymorphism.

There is an example of this in the documentation:

CShape[10] shapes;                       // массив объектов CShape
//... здесь заполняем массив производными объектами
for  (int i=0; i<10;i++)  
  {
   //--- тип и площадь фигуры
   Print("Объект типа "+shapes[i].GetType()+" имеет площадь "+ shapes[i].GetArea());
  };

I'm not quite sure what should be in place.

//... здесь заполняем массив производными объектами

Can I have at least 1 example?

Before your example from the documentation, there is this phrase: "Now we can declare an array of Shape type and fill it with objects of derived classes. So the shapes[10] array should be filled with instances of classes derived from CShape. Right?

There are two examples of derived classes in the same documentation: CCircle and CSquare. So it remains for us to create instances of these two classes, for example:

CCircle  objCircle;
CSquare  objSquare;

... and fill the first two elements of our array shapes[10] with them:

shapes[0]=objCircle;
shapes[1]=objSquare;

Do you agree?

 
Yedelkin:

Before your example from the documentation, there is this phrase: "Now we can declare an array of type Shape and fill it with objects of derived classes". Therefore, array shapes[10] should be filled with instances of classes derived from CShape. Right?

There are two examples of derived classes in the same documentation: CCircle and CSquare. So it remains for us to create instances of these two classes, for example:

... and fill the first two elements of our array shapes[10] with them:

Do you agree?

I may agree, but the compiler generates an error:

'shape' - structure has objects and cannot be copied test.mq5 36 3

 
equivalent23:

I might agree, but the compiler generates an error:

'shape' - structure has objects and cannot be copied test.mq5 36 3

Can you give me the full code?
 
Yedelkin:
Can you quote the full code?

Sure:

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                                               Ya |
//|                                                               Ya |
//+------------------------------------------------------------------+
#property copyright "Ya"
#property link      "Ya"
#property version   "1.00"
#property indicator_chart_window
//--- Базовый класс
class CShape
  {
   protected:
      int            m_type;                // тип фигуры
      int            m_xpos;                // X - координата точки привязки
      int            m_ypos;                // Y - координата точки привязки
   public:
      void           CShape(){m_type=0;};   // конструктор, тип равен нулю
      int            GetType(){return(m_type);};// возвращает тип фигуры
   virtual
      double         GetArea(){return (0); }// возвращает площадь фигуры
  };
 
  //--- производный класс Круг
class CCircle: public CShape          // после двоеточия указывается базовый класс,
  {                                      // от которого производится наследование
private:
   double         m_radius;              // радиус круга
 
public:
   void           CCircle(){m_type=1; m_radius=10;};  // конструктор, тип равен 1
   void           SetRadius(double r){m_radius=r;};
   double GetArea(){return (3.14*m_radius*m_radius);}// площадь круга
  };
 
  //--- производный класс Квадрат
class CSquare: public CShape          // после двоеточия указывается базовый класс,
  {                                      // от которого производится наследование
private:
   double          m_square_side;        // сторона квадрата
 
public:
   void            CSquare(){m_type=2;}; // конструктор, тип равен 2
   void            SetSide(double s){m_square_side=s;};
   virtual double GetArea(){return (m_square_side*m_square_side);}//площадь квадрата
  };
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

 
  CShape shape[10];                       // массив объектов CShape
//... здесь заполняем массив производными объектами
  CCircle circle;
  shape[1]=circle;
 
   Print("Объект типа "+shape[1].GetType()+" имеет площадь "+ shape[1].GetArea());
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 
 

Reason: