Download MetaTrader 5

How to override Compare() in CObject so CList sort() works? - page 4

To add comments, please log in or register
Gustavo Hennemann
214
Gustavo Hennemann  
Ex Ovo Omnia:

I am just curious why you needed the operator "&" in the function Compare() to get the value of "this".


Because the method Compare() is "const", and is not possible call non "const" methods inside it.

In this example, see code in page 2, the method getPrice() is not const and inside it the function CopyClose() also is not const. All the methods can be chaged to "const", but CopyClose() not.

Anyway, the operator "&" gives acces to methods non const, so, we can call them inside a "const" method.

Ex Ovo Omnia
3343
Ex Ovo Omnia  
Gustavo Hennemann:

Because the method Compare() is "const", and is not possible call non "const" methods inside it.

In this example, see code in page 2, the method getPrice() is not const and inside it the function CopyClose() also is not const. All the methods can be chaged to "const", but CopyClose() not.

Anyway, the operator "&" gives acces to methods non const, so, we can call them inside a "const" method.


I see now. I have no idea, if it is a valid construction, probably you know. For me it is better avoiding 'const' method declarations completely in MQL, like in Java.

nicholishen
883
nicholishen  
Ex Ovo Omnia:

I see now. I have no idea, if it is a valid construction, probably you know. For me it is better avoiding 'const' method declarations completely in MQL, like in Java.


In order to utilize the (very useful) methods of the std library's collection classes (ie. search, sort,etc.) you have to override the Compare method of CObject. This means that all methods called from within the Compare method also have to be const methods. OP didn't circumvent this detail, OP just didn't use the 'this' pointer correctly. So instead of simply calling this.DoSomething(), OP created his own pointer to (self)object. eg. otherSelfPointer.DoSomething()

Ex Ovo Omnia
3343
Ex Ovo Omnia  
nicholishen:

In order to utilize the (very useful) methods of the std library's collection classes (ie. search, sort,etc.) you have to override the Compare method of CObject. This means that all methods called from within the Compare method also have to be const methods. OP didn't circumvent this detail, OP just didn't use the this pointer correctly. So instead of calling this.DoSomething(), OP created his own pointer to (self)object instead of simply using the this keyword.  eg. otherSelfPointer.DoSomething()


I understand what he did, the question is now, if this construction is harmless (in general). The compiler does not expect that the object modifies its members in const functions.

Gustavo Hennemann
214
Gustavo Hennemann  
Ex Ovo Omnia:

I understand what he did, the question is now, if this construction is harmless (in general). The compiler does not expect that the object modifies its members in const functions.

In this specific case I only getting the value, I'm not changing anything. But, if we try to change a const value probably the compiler will alert about this issue, or, in the worst case we will get a run time exception.

nicholishen
883
nicholishen  
Ex Ovo Omnia:

I understand what he did, the question is now, if this construction is harmless (in general). The compiler does not expect that the object modifies its members in const functions.


Ohhh.... I see now, my bad. That could potentially be dangerous. OP should make sure that all methods within the perview of Compare() are also const methods.

To add comments, please log in or register