Discussion of article "Using the Object Pointers in MQL5" - page 2

 

Good afternoon. I am a beginner. I have a question regarding the use of the "*" icon (asterisk).

In the samplelist file:

class CListItem
  {
private:
   int               ID;
   CListItem        *next;
   CListItem        *prev;

"*" is used to declare a pointer to an object - this is kind of clear.

Next:

public:
                    ~CListItem();
   ...
   CListItem*        next(){return(next);}
   CListItem*        prev(){return(prev);}

It is not clear what the "*" sign is used for.

According to the help,"it is possible to dynamically create objects of a complex type. This is done with the help of the new operator." The new operator is not used here. Please explain in more detail how and in what cases the "*" sign can be used.

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

Further, however:

It is not clear what the "*" sign placed there means.


You can write like this

public:
                    ~CListItem();
   ...
   CListItem        *next(){return(next);}
   CListItem        *prev(){return(prev);}

There will be no difference, it concerns only the style of design and a small ideological approach.

 
Rosh:

You can write it like this

There will be no difference, it only concerns the style of design and a small ideological approach.

Ie.

CListItem        *next(){return(next);}

this is a declaration of a pointer to the object (or method?) next()? And then, in curly brackets, the object next itself is returned? Do I understand correctly?

I understand that everything should be very simple, but since I haven't encountered OOP before, I'm wandering in 3 pines. Please make me understand.

 
kogriv:

I.e..

is a declaration of a pointer to the object (or method?) next()?

It says that the next() function returns a pointer to a variable of the CListItem class.
 
kogriv:

And then, in curly braces, the next object itself is returned? Do I understand correctly?

The body of the function is given in curly braces,

{return(next);}

it means that the function will return the variable next, which has the type of a pointer to an object of the CListItem class. This generally agrees with the type of the function.

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

Right after the first example, the article contains the phrase "The pstatus variable is a pointer of an object, but we deliberately "forgot" to create the object itself using the new operator and passed it as a parameter to the PrintStatus() function".

1. It's unclear what "exactly" you passed - a pointer or an object?

2. There is no PrintStatus() function in the example.

 

I think there is an error:

//+------------------------------------------------------------------+
//| adding an item to the list|
//+------------------------------------------------------------------+
CList::addItem(CListItem *item)
  {
//--- first check the correctness of the passed pointer
   if(CheckPointer(item)==POINTER_INVALID) return;
//--- increase the count of items in the list
   m_counter++;
//--- if there are no items in the list yet
   if(CheckPointer(first)!=POINTER_DYNAMIC)
     {
      first=item;
     }
   else
     {
      //--- set item to the pointer of the previous object----------------------- Start of error-----------------------
      item.prev(first);
      //--- remember the pointer of the current first element
      CListItem *p=first;
      //--- put the incoming item in place of the first item
      first=item;
      //--- set the pointer of the next object for the first element of the list
      first.next(p);
     }
  } 
It appears that the new list element has prev and first referencing the second element, and the second element does not get a reference to the first element.
 
Yedelkin:

Right after the first example, the article contains the phrase "The pstatus variable is an object pointer, but we deliberately "forgot" to create the object itself with the new operator and passed it as a parameter to the PrintStatus() function".

1. It is unclear what exactly was passed - a pointer or an object?

2. There is no PrintStatus() function in the example.

Better late than never. Thanks, fixed in the article. The end of the sentence has been removed

Right after the first example, the article contains the phrase "The pstatus variable is a pointer of an object, but we deliberately "forgot" to create the object itself using the new operator and passed it as a parameter to the PrintStatus() function".

 
220Volt:

I think there is an error:

It appears that the new list element has prev and first referencing the second element, and the second element doesn't get a reference to the first element.

Yes, there's a typo. Instead of

 else
     {
      //--- set item  указатель предыдущего объекта                                         
      item.prev(first);

should be

else
     {
      //--- set for first указатель предыдущего объекта
      first.prev(item);
corrected.
 

I still don't understand about pointers and references in MQL5 and now in MQL4. What is the difference between passing by reference and pointer except for extra code? There is a difference in C++, but what is it here? If it is not difficult, please write more detailed information.