You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi everyone -
So, my MQL4 days are slowly coming to an end. My broker has implemented MQL5 and now, the conversion from 4->5 begins. When I started MQL4, I was an OOP noob and in some respects, I still am. Building OOP classes has certainly paid huge dividends by creating a foundation of thoroughly tested and extensible objects ultimately providing substantial increases in areas of stability and performance and by limiting the number of code rewrites and duplication through polymorphism. As I venture into the MQL5 arena, I want to know that I adhere to customary design standards and standard/best practices for OOP programming; hence my conundrum.
Given the follow following class used to store session details:
The code is operational as from the testevent.mqh EA as follows:
The results of the first tick show:
I love the operator overload! This represents the implementation of the CEvent class from within CSession. It provides a straightforward of "speaking", i.e., "Q: Is the session in reversal?" Code: if (session[NewReversal])... Truly, easy to understand and streamlined code. I guess the "proper" OOP approach is to implement a public method, e.g.:
Then, access the data from the EA as follows (which, I admit, from a purist perspective is more "proper"):
But, this is not my quandary. The specifics of my venture lie in the distinction between class methods and class properties. Class data needs to be both immutable and and public. Say, for example, I ask the question, "Q: Is the session trading on its highs or trading on its lows?" the question is answered by implementing the following method:
And I would then ask the question in code as:
Not bad, however, this approach seems to imply that for every immutable class property a method is required. Further noted, the '()' operator specifically describes a method 'function' implying the possibility that the value .Direction() is derived from a number of variables as opposed to the simple return of a private variable as presented. Using this approach, the code requires a substantial number of class property methods as follows:
Resulting in code that looks that likes this:
So, my quandary is: Is there a coding practice, standard, or approach to expose immutable (private/protected) class properties simply using '.' notation without having to define a method for each property? Given that I completely buy into the fact that the class data only be modified by class methods, I'm hoping I missed something in terms of exposing class properties that permits notation along the lines of: