The source code in the text should be brought in line with the attached files, in particular in the listing GetCriticalError_Unsafe.mq5 the reference is made to the non-existent variable status instead of pstatus (as in the file).
And what is the point of defining its own variable type in each of the CShape heir classes?
And I would also like to hear explanations about this. When switching from the GetCriticalError_OnDemand example to GetCriticalError_Unsafe, the whole change was practically reduced to changing the type of the function argument from CHello *pobject to CHello &pobject. At the same time, the function call remained unchanged - a pointer (CHello *) was passed there. In this case, according to the author's words,"we get a critical error again. The point is that if an object is passed by reference, the critical error occurs at the stage of calling a function where an uninitialised object is passed into it". But in this case it is a pointer and not a reference that is passed to the function. The question arises where it comes from. Could you please elaborate on the rules used by MQL5 for implicit conversion of pointers into references and back? Or point to the place in the documentation where it is described. If the compiler supported strict typing, the example would simply fail to compile due to the lack of a function with an argument of the required type.
The source code in the text should be brought in line with the attached files, in particular in the listing GetCriticalError_Unsafe.mq5 reference is made to a non-existent variable status instead of pstatus (as in the file).
Thanks, corrected in the article and in the file.
What is the point of defining its own type variable in each of the CShape successor classes?
I would also like to hear some clarifications on this issue. When switching from the GetCriticalError_OnDemand example to GetCriticalError_Unsafe, the whole change was practically reduced to changing the type of the function argument from CHello *pobject to CHello &pobject. At the same time, the function call remained unchanged - a pointer (CHello *) was passed there. In this case, according to the author's words,"we get a critical error again. The point is that if an object is passed by reference, the critical error occurs at the stage of calling a function where an uninitialised object is passed into it". But in this case, a pointer is passed to the function, not a reference. The question arises where it comes from. Could you please elaborate on the rules used by MQL5 for implicit conversion of pointers into references and back? Or point to the place in the documentation where it is described. If the compiler supported strict typing, the example would simply fail to compile because there is no function with an argument of the required type.
In principle, this member is not used anywhere, you can remove it. Theoretically, it can be used in descendants to implement functions that depend on the object type.
Is there somewhere I can read about the direction of implicit conversions performed on references and pointers? In particular, is the inverse conversion also done - from a reference to a pointer?
- www.mql5.com
Do I understand correctly that according to the current source of the example we get a "rake" that in the object of a shape of a particular type there are two type variables, for example, CLine::type in addition to CShape::type? Ideally there should be one - from the base class.
Yes, you are right. There are two type variables in each descendant as you wrote. It happened this way because the code of descendants was copied. To make a member of the base class available directly to descendants, it should be defined with the protected specifier or public get- and set-methods for this member should be declared in the base class.
For example, like this:
//+------------------------------------------------------------------+ //|demo_inheritance.mq5 | //| Copyright 2010, MetaQuotes Software Corp. | | //|http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2010, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //|| //+------------------------------------------------------------------+ class CBase { private: int type; public: CBase(){type=0;} int GetType(){return(type);} }; //+------------------------------------------------------------------+ //|| //+------------------------------------------------------------------+ class CSon: CBase { private: int type; public: CSon(){type=2;} void PrintType(); }; //+------------------------------------------------------------------+ //|| //+------------------------------------------------------------------+ void CSon::PrintType() { Print("CSon.type =",type); Print("CBase.type =",CBase::GetType()); } //+------------------------------------------------------------------+ //| Script programme start function| //+------------------------------------------------------------------+ void OnStart() { //--- CSon son; son.PrintType(); int father_type=son.GetType(); } //+------------------------------------------------------------------+
"This example is very simple, it is not difficult to find an error in it. But if your mql5-program contains hundreds or even thousands of lines, catching such errors may become much more complicated. Especially for those cases when the conditions of abnormal situations in the program's behaviour depend on unpredictable factors - for example, on a certain market."
How?:-)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article Using the Object Pointers in MQL5 is published:
By default, all objects in MQL5 are passed by reference, but there is a possibility to use the object pointers. However it's necessary to perform the pointer checking, because the object may be not initialized. In this case, the MQL5 program will be terminated with critical error and unloaded. The objects, created automatically, doesn't cause such an error, so in this sence, they are quite safe. In this article, we will try to understand the difference between the object reference and object pointer, and consider how to write secure code, that uses the pointers.
Author: MetaQuotes