Errors, bugs, questions - page 1991

 
Alexey Kozitsyn:
Add a default constructor to the structure.

Forum on trading, automated trading systems and trading strategy testing

Bugs, bugs, questions

fxsaber, 2017.09.06 09:38

// Нужен только для того, чтобы показать необходимость решения без конструктора
union UNION
{
  STRUCT Struct; // конструктора не должно быть
  int i;
};
 
fxsaber:

Inattentive me:) Can you explain why this is needed? Why can't the constructor be used?

Or is it to show an error of structure initialization with a closed field?

 
Alexey Kozitsyn:

Inattentive me:) Can you explain why this is needed? Why can't the constructor be used?

Because when writing my own code I came across a situation where my structure is involved in a union. The solution with an empty constructor came immediately to mind but it was a bummer that my code had a union. That's why I was asking this question not for the sake of boring me but for practical use.


I had to write this twist to avoid this "bug" (is it a bug or not?).

// Зануляем структуру без конструктора со скрытыми полями
template <typename T>
void StructToNull( T& Struct )
{    
  union TMP_UNION
  {
    T Struct;
    
    struct TMP_STRUCT
    {
      uchar Array[sizeof(T)];
    } Tmp;
    
    TMP_UNION( T& Value )
    {
      ::ArrayInitialize(this.Tmp.Array, 0);
      
      Value = this.Struct;
    }
  } TmpUnion(Struct);    
}


I haven't seen any other solution yet.

 
fxsaber:
Run it, saw the problem. It seems that only structures with public fields are allowed with unions now. Then it would be possible to write struct = {0};
 
Alexey Kozitsyn:
Run it, saw the problem. It seems that only structures with public fields are allowed with unions now. Then it would be possible to write struct = {0};

This is not the case.

 

Maybe I misunderstood you again, but this code works without any warnings:

struct STRUCT
{
//private:
  int i;

};

// Нужен только для того, чтобы показать необходимость решения без конструктора
union UNION
{
  STRUCT Struct; // конструктора не должно быть
  int i;
};

// Обходим Warning "possible use of uninitialized variable"
template <typename T>
void StructInit( T& ) {}

STRUCT Func()
{
  STRUCT Res = {0}; // ок!
  
//  StructInit(Res); // с этой строкой Warning не появляется, но это какой-то абсурд!
  
  return(Res);  
}

void OnStart()
{
  Func();
}
 
Alexey Kozitsyn:

Maybe I misunderstood you again, but this code works without any warnings:

That's right, the public structures are initialized. But union has nothing to do with it.


Interestingly, if a structure with a hidden field is made a public field of a new structure, then the new one will not initialize either, even if all fields are open.

 

Hi, could you tell me if it's possible to declare an array by specifying the number of elements in it with a variable? If not, how can this be done?

extern int Period = 10;
int Massiv[Period]; //Выдаёт ошибку: "invalid index value"
 
giros:

Hi, could you tell me if it's possible to declare an array by specifying the number of elements in it with a variable? If not, how can this be done?

https://www.mql5.com/ru/docs/array/arrayresize

Документация по MQL5: Операции с массивами / ArrayResize
Документация по MQL5: Операции с массивами / ArrayResize
  • www.mql5.com
Операции с массивами / ArrayResize - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
giros:

Hi, could you tell me if it's possible to declare an array by specifying the number of elements in it with a variable? If not, how can this be done?


ArrayResize()

Reason: