Finally, we are at our final requirement implementation for this simple list.
5. Allow to access the objects as if we were using an array, example MyList[0] .
I voluntarily introduced this point in order to illustrate another powerful feature of the new MQL language or OOP programming in general which is the possibility of overloading operators.
But first let me explain what those two words mean.
An operator is a keyword or character through which you make some operation on a variable or variables. Examples: +, -, =, ==, etc…
For more info, have a look here.
From that list, there is one which is of interest for us, the indexing operator '[]'.
Overloading is the ability to redefine the behavior of such operators in the context of the classes in which they are declared. That means that if we want to use the indexing operator within our List class, we have to declare the operator inside our class as we do for any other method.
Here is how our indexing operator must be declared inside our List class:
We have just declared a method for the indexing operator. It accepts an integer 'index' that will be used to access the items in the list and returns a pointer to the item.
Let's give it a body:
And finally here is how to use this indexing operator with our MyList object we created in our indicator.
Our last point 5 from our requirements list is implemented so I consider the implementation of our List terminated.
Attached are the final files.
5. Allow to access the objects as if we were using an array, example MyList[0] .
I voluntarily introduced this point in order to illustrate another powerful feature of the new MQL language or OOP programming in general which is the possibility of overloading operators.
But first let me explain what those two words mean.
An operator is a keyword or character through which you make some operation on a variable or variables. Examples: +, -, =, ==, etc…
For more info, have a look here.
From that list, there is one which is of interest for us, the indexing operator '[]'.
Overloading is the ability to redefine the behavior of such operators in the context of the classes in which they are declared. That means that if we want to use the indexing operator within our List class, we have to declare the operator inside our class as we do for any other method.
Here is how our indexing operator must be declared inside our List class:
Inserted Code
ListNode* operator[](int index);
We have just declared a method for the indexing operator. It accepts an integer 'index' that will be used to access the items in the list and returns a pointer to the item.
Let's give it a body:
Inserted Code
/**************************************************************************************** operator []( int index ) Returns the element specified by index or NULL if the element is not in the list ****************************************************************************************/ ListNode* List::operator[]( int index ) { ListNode* pN = m_pBegin; for( int i=0; pN != NULL && i<index; i++, pN = pN.Next() ) { } return pN; }
And finally here is how to use this indexing operator with our MyList object we created in our indicator.
Inserted Code
int ListCount = MyList.Count(); for( int i=0; i<ListCount; i++ ){ // We have to cast ListNode* to ListedObject* in order to use the Name()method Print( ((ListedObject*)(MyList[i])).Name() ); }
Our last point 5 from our requirements list is implemented so I consider the implementation of our List terminated.
Attached are the final files.
Attached File(s)
SimpleListSbS.mqh
7 KB
|
380 downloads
SimpleListSbS.mq4
4 KB
|
409 downloads
Simplicity is the Ultimate Sophistication.