PLO. Application issues - page 2

 
Urain:

The new operator creates an instance of the class and a constructor is called in conjunction with it. It is written in the syntax, so there is no other way to call it.

I proceeded from this example:

//+------------------------------------------------------------------+
//| Создание фигуры                                                  |
//+------------------------------------------------------------------+
void CTetrisField::NewShape()
  {
   m_ypos=HORZ_BORDER;
//--- случайным образом создаём одну из 7 возможных фигур
   int nshape=rand()%7;
   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;
     }
Roughly speaking, there are no parentheses (constructor call) here. It's possible that I'm still missing something.
 
Urain:

This code is almost no different from multiple inheritance.

In fact, class C_C has access to data of C_A and C_B through appropriate pointers.


This is called aggregation
 
Yedelkin:

I was based on this example:

Here, roughly speaking, there are no parentheses (constructor calls). It's possible that I don't understand something yet.

Why me, it's not for me, it's for central laundry, I didn't take brackets :o)
 
Urain:
Why me, it's not for me, it's for the central laundry, I didn't use brackets :o)

My compiler doesn't complain about missing parentheses...

      case  50: d_Control=new CControl_50; break;

Anyway, a question suddenly arose: what is the right way to do it? Or it makes no difference whether there are brackets or not?

 
Yedelkin:

My compiler doesn't complain about missing parentheses...

Anyway, a question suddenly arose: what's the right way? Or it makes no difference if there are brackets or not?

If it works both ways, it's rather a matter of syntax unification. The main thing is that it doesn't affect speed.

ZS And does it work at all? To be honest I have not checked, I always write with parentheses.

 
Urain:

If it works both ways, it's more a matter of syntax unification. The main thing is not to affect the speed.

ZS Does it work at all? To be honest I haven't checked, I always write with brackets.

So far I can only talk about compilation results. It's still a long way to go to check if the code actually works.
 

I haven't read the description of creating class objects dynamically in MQL5, but in C++ you can do it without brackets (standard constructor is called) and with brackets (constructor is called depending on parameters in brackets). Example:


class CExample
{
    int param;
public:
    CExample();
    CExample(int x);
    ~CExample();
}

CExample::CExample(int x)
{
    param = x;
}

//..........вызов..............
//..где-то в тексте программы..

   CExample *ex1 = new CExample;         //создание с конструктором CExample();
   CExample *ex1 = new CExample();       //создание с конструктором CExample();
   CExample *ex1 = new CExample(value);  //создание с конструктором CExample(int x);

Документация по MQL5: Основы языка / Типы данных / Структуры и классы
Документация по MQL5: Основы языка / Типы данных / Структуры и классы
  • www.mql5.com
Основы языка / Типы данных / Структуры и классы - Документация по MQL5
 
In mql5, you cannot pass parameters to the constructor.
 
Vigor:
In mql5 no parameters can be passed to the constructor yet.

clarify " In mql5 no parameters can be passed to constructor yet ".
 
PiramidaR:

I haven't read the description of creating class objects dynamically in MQL5, but in C++ you can do it without brackets (standard constructor is called) and with brackets (constructor is called depending on parameters in brackets). Example:

Got it, thanks for the simple and straightforward explanation. It turns out that parentheses are for possible parameters.

And the description of how to create dynamic class objects in MQL5 is very concise:

MQL5 Reference / Language Basics / Operators / Object creation operator new

MQL5 Reference / Language Basics / Data Types / Object Pointers