Learning and writing together in MQL5 - page 32

 

I don't know :) at our office for written code like

std::string s = "";

at the very least, they will be scolded, and rightly so, imho.

Again, this is a complex type that requires correct initialization at creation, if the string needs to be initialized, it's a bug of MT5(4), for sure.

Maybe the size of arrays should be zeroed immediately after creation?

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

I don't know :) In our company, they will get a fine for writing code like

At the very least, you will be reprimanded, and rightly so, I think.

Before you can use a variable, you must assign a value to it (explicitly initialize it). Or don't you agree?

Explicit initialization doesn't imply the need for it at the moment of declaration. But if the uninitialized variable's value is then used in some calculations, the consequences are unpredictable.

 

You scare me, Rashid, with all due respect.

A class has a constructor, which performs the initial initialization of the object and including setting default value, if it's logical.

If the initial initialisation doesn't happen, there must be a way to know if the object is in an adequate state. With a string it makes much more sense to set a default value.

std::string s = "";

Such notation is paranoia, a paranoid person cannot write normal code a priori.

So strings are not initialized?

 
TheXpert:
So the strings are not initialised?
Of course they are initialised, but not to the empty string "", but to NULL.
 
TheXpert:

You scare me, Rashid, with all due respect.

A class has a constructor, which is where the initialization of the object takes place and includes setting a default value, if that makes sense.

If the initial initialisation doesn't happen, there must be a way to know if the object is in an adequate state. With a string it makes much more sense to set a default value.

Such notation is paranoia, a paranoid person cannot write normal code a priori.

So strings aren't initialised?

Read the help - https://www.mql5.com/ru/docs/basis/types/classes:

If a structure contains variables of string type and/or a dynamic array object , the compiler assigns an implicit constructor to such a structure, in which all members of thestringtype structure are zeroed and a correct initialization is done for the dynamic array object.

...

Constructors and destructors

A constructor is a special function that is called automatically when creating a structure object or a class, and is usually used to initialize class members. In the following discussion we will only talk about classes, the above also applies to structures, unless otherwise stated. The constructor name must coincide with the class name. The constructor has no return type (you can specify void type).

Certain class members - strings, dynamic arrays and objects requiring initialization - will be initialized anyway, regardless of the presence of a constructor.

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

That's a bummer... At least I'll know.

Will it be NULL in structures too?

 
TheXpert:

That's a bummer... Well at least I'll know.

Will it be NULL in structures as well?

Just compare to NULL and you will know everything yourself - Type void and constant NULL:

The predefined constant variable NULL is ofvoid type . It can be assigned to variables of any other fundamental types without conversion. It is also allowed to compare variables of fundamental types toNULL.

Example:

//--- if a string is not initialized, assign our predefined value to it
if(some_string==NULL) some_string="empty";

NULL canalso be compared to pointers to objects created using the new operator.

 
Rosh:

Before a variable can be used, it must be assigned a value (explicitly initialized). Or don't you agree?

Explicit initialization doesn't imply the need for it at the moment of declaring the variable. But if an uninitialized variable's value is subsequently used in some calculations, the consequences are unpredictable.

Speaking of birds.

//Struct StrMQL4_Deal
struct StrMQL4_Deal
{
//----------------------------------------------------------------------------//
double TP = 0;
double SL = 0;
//----------------------------------------------------------------------------//
};

And how will the compiler respond? And it will answer the following '=' - illegal assignment use

//Class CMqlExpert
class CMqlExpert
//Purpose: Main class of trade system. 
{
//----------------------------------------------------------------------------//
protected:
//****************************************************************************//
//                    Protected declarations of the class                     //
//****************************************************************************//
bool TradeMonday    = true; //Trade in monday
bool TradeTuesday   = true; //Trade in tuesday
bool TradeWednesday = true; //Trade in wednesday
bool TradeThursday  = true; //Trade in thursday
bool TradeFriday    = true; //Trade in friday
//----------------------------------------------------------------------------//
public:

The compiler wasn't bent - '=' - illegal assignment use.

My question is - What will happen if the programmer (in this case, me) doesn't initialize variables before using them (he does it the way the compiler requires and omits this moment in the constructor)?


 
Interesting:

I have a question - What happens if the programmer (in this case me) doesn't take care of initializing the variables before using them (by doing so as required by the compiler and missing this point in the constructor)?

The question comes from a beginner...
 
sergeev:
Sounds like a question from some newcomer...

Well, that's what I thought at the time (it was a long time ago, it comes to mind), but I wanted to write an application in the heat of the moment. :)

Although I still don't understand why it's forbidden to initialize variables at compiler level.

Reason: