Questions from Beginners MQL5 MT5 MetaTrader 5 - page 179

 
Reshetov:

This is not a pointer to itself, but a pointer associated with the field identifier in the object implementation.

I.e. there may be a local variable with the same identifier as the field in some object method, in which case you can use this to distinguish a pointer to the object field from a pointer to a local variable.

what purpose could this be used for? can you use this as an example?
 
Barbarian2:
What purpose could this be used for? Can you give me an example?
This is most often used in class constructors, so as not to make up new identifiers, because it is in constructors that argument values are assigned to object fields.
 
Barbarian2:
but for what purposes this may be needed? can you give an example?
class node
{
   public:
      void LinkWithNode(node* childNode)
      {
          if(CheckPoint(linkNode) != POINTER_INVALID)
             this.childNode = childNode;
      }
   private:
      node* childNode;
};

class LineNode : node
{
   public:
      LineNode(node* parentNode)
      {
         parentNode.LinkWithNode(GetPointer(this));
      }
};

In this case, when creating a LineNode, you need to specify the parent node to which the current LineNode instance belongs. The parent node will then be able to refer to this instance by reference. In this example, the word this is used twice. In the first case, the word this specifies that the childNode variable refers specifically to the current class, while the childNode without this is a LinkWithNode method variable of the same name. In the second case, this returns the current instance of the class and GetPointer receives a pointer to it. This allows you to bind a LineNode at the moment of creation.

 
C-4:

In this case, when creating a LineNode, you need to specify the parent node to which the current LineNode instance belongs. The parent node will then be able to refer to this instance by reference. In this example, the word this is used twice. In the first case, the word this specifies that the childNode variable refers specifically to the current class, while the childNode without this is a LinkWithNode method variable of the same name. In the second case, this returns the current instance of the class and GetPointer receives a pointer to it. Thus it is possible to bind LineNode at the moment of creation.

i.e. in second case it will lead to code reduction?
 
Barbarian2:
i.e. in the second case, it will lead to code reduction?
Whatever the shorthand (in this case), you're focusing on the wrong thing. The use of this can be varied. The important thing is to understand that this is a synonym for the object instance where this is used. When defining a class, when there are no instances of this yet, this is the only way to tell the compiler what the programmer means.
 
Barbarian2:
i.e. in the second case, it will lead to code reduction?

No, that's not it. This allows us to control an instance of a class already in its definition. Knowing this, we can impose our own unique behaviour on a particular instance. This achieves a high degree of flexibility and abstraction.

 

A word of advice. I'm moving from mql4 to 5, I'm going to write an indicator for 1000 or 10k buffers. Is it possible to name the buffers with variables?

Like

while( a<1000)

{

a++ B++

buffer name = buff+a

buff(a)=iMA(null, B,) }

And subsequent buffer processing with the same looping

Or any other way to reduce time and code with new mql5 features.

 
Andrei-1:

First of all, the limit is 256 buffers (I could be wrong).

And buffers are ok, you can put them in classes, give aliases and search by aliases.

 
Thank you, we will look into it.
 
Andrei-1:

Somewhere in articles tol64 saw, I can not remember at a glance, look for it.

TheXpert:

First of all, the limit is 256 buffers (I could be wrong).

And everything is OK with buffers, you can put them into classes, give them aliases and search by aliases.

There's no restriction in the help"The number of indicator buffers you can use in a custom indicator is not limited. ", but it did mention 512 somewhere, so I don't know.
Reason: