The EOP for schoolchildren. - page 16

 

I ask the moderators to put all the discussions in a separate thread. There are probably dozens of such threads discussing the advantages of OOP.

 
Alexey Viktorov:

One more question: What is the difference between declaring a variable, getting/creating a pointer, creating an object instance. It's not the difference, but in which cases it's better to use. For example, it's enough to declare a variable for opening a position.

And in what cases it's better to use the pointer, and in what cases we can't do without the object instance.

What are the pros and cons of one method or another?

1. If an object is in the global scope, it's a static object. It is placed in static memory and exists as long as the program exists.

Its constructor is called before the call of the first function of the program. The destructor is called after OnDeinit().

2. Objects declared inside the function are local objects. They are placed in the stack. They are destroyed on exiting the function.
You can declare a local object inside curly braces.


For example, like this:

if( ... )
{
  MyClass object;
  ...
}  //  Здесь вызывается деструктор.

Or even like this:

{
  MyClass object;
  ...
}  //  Здесь вызывается деструктор.

It will be destroyed on exiting the block. When it is destroyed, the destructor is called.

Since the size of the stack is usually limited, very large objects cannot be placed in the stack.

3. Objects created by the new operator are in dynamic memory (heap).
They will persist until they are explicitly eliminated by the delete operator.


Example:

void func()
{
  Myclass* pObject = new MyClass();
}

In this example, two variables are created. An object of MyClass class in heap.

And the pObject variable is a pointer to MyClass. This variable is assigned the address of the object in dynamic memory.

When exiting from the function, the pObject variable will be destroyed, but the object will remain in dynamic memory.

In order to be able to delete it, its address must be stored somewhere.

When they are created and destroyed, the constructor and destructor are also called.

Dynamic variables typically take longer to access than local variables.


You should take all this into account when creating an object.


 
Koldun Zloy:

1. If an object is in the global scope, it is a static object. It is placed in static memory and exists as long as the programme exists.

...

And no one will say anything, not even ZyXpert.
 
Koldun Zloy:

Am I correct in assuming that with this option

#include <Trade\Trade.mqh>
CTrade trade;

int OnInit()
{
 trade.SetExpertMagicNumber(123);
}

void OnTick()
{
 CTrade trade;
 trade.Buy(0.1);
}

without considering the compiler warning, the position may open with a magic number not equal to 123 ?

That is, a new object will be created in the OnTick function and the magician will not be equal to 123 in it.

 
Alexey Viktorov:

Am I correct in assuming that with this option

without considering the compiler warning, the position may open with a magic number other than 123 ?

That is, a new object will be created in the OnTick function and the magician will not equal 123 in it.

SetExpertMagicNumber

 
Alexey Viktorov:

Am I correct in assuming that with this option

without considering the compiler warning, the position can open with a magic not 123 ?

That is, a new object will be created in the OnTick function and it will not have a MagicNumber of 123.

Yes, that is correct. These are two different objects. Since the local object in OnTick has the same name as the global object, only it can be accessed in the OnTick function. The global object is out of scope. Therefore, for the local trade object, the MagicNumber has not yet been set.

 
Alexey Viktorov:

Am I correct in assuming that with this option

#include <Trade\Trade.mqh>
CTrade trade;   // Объект № 1 в глобальной области программы

int OnInit()
{
 trade.SetExpertMagicNumber(123);
}

void OnTick()
{
 CTrade trade;  // Объект № 2 в локальной области OnTick()
 trade.Buy(0.1);
}

without considering the compiler warning, the position can open with a magic not 123 ?

That is, a new object will be created in the OnTick function and the magician will not be 123 in it.

It's exactly the same as with variables.

And focus:

#include <Trade\Trade.mqh>
CTrade trade;   // Объект trade в глобальной области программы

int OnInit()
{
 trade.SetExpertMagicNumber(123);
}

void OnTick()
{
 CTrade *trade_ptr=GetPointer(trade);  // Указатель на объект trade
 trade_ptr.Buy(0.1);
}
 
Why is thisSetExpertMagicNumber needed in the first place?
 
Сергей Таболин:
Why is thisSetExpertMagicNumber needed in the first place?

It sets a magic number to the object of trade class. There can be more than one CTrade class object for the whole program as it is done now in kodobase. This is an object. This object is used to performtrading operations. There is no need to create one object for the whole program and reset it every time a new magician, slippage and other parameters that are set during initialization need to be changed. It is possible to create a separate trading object for each traded symbol, set necessary parameters for it according to the properties of the symbol, to which this specific object of the CTrade class belongs, and easily trade by getting a pointer to the necessary object of the necessary trading class from the necessary symbol, without overriding the parameters set at that.
To work with another magician, a separate trade object with appropriate settings can be defined. There can be several wizards in one EA.

It all depends on understanding of what and how we use it.

 
Artyom Trishkin:

It sets a magik for an object of the trade class. There can be not only one object of CTrade class for the whole program as it is usually done in kodobase. This is an object. This object is used to perform trading operations. There is no need to create one object for the whole program and reset it every time a new magician, slippage and other parameters that are set during initialization need to be changed. It is possible to create a separate trading object for each traded symbol, set necessary parameters for it according to the properties of the symbol, to which this specific object of the CTrade class belongs, and easily trade by getting a pointer to the necessary object of the necessary trading class from the necessary symbol, without overriding the parameters set at that.
To work with another magician, a separate trade object with appropriate settings can be defined. After all, there can be several wizards in one EA.

It all depends on understanding of what and how we use it.

In that case, is it

SetExpertMagicNumber
Устанавливает идентификатор эксперта

incorrect description?

My understanding was that if set for EA, then any orders/positions should have this magik. ((