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

 
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.

 
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.

 
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()

 
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.

 
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.

 
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.

 
Gustavo Hennemann:

Hi @Alain Verleyen,

I changed the method getPrice(), insted using CopyClose() I'm using CopyBuffer(). This doesn't change the main objective.

So, if I use "const" keyword in the method getPrice(), a get the error: "'CopyBuffer' - no one of the overloads can be applied to the function call". I think this occurs because CopyBuffer is not a const method, and is not possible call non const method inside a const method.

Sorry for late reply, but I had no time to follow the topic up to now.

I still don't see why you could not declare your getPrice() as const when using CopyBuffer(). Doesn't make sense to me, could you post your updated version of getPrice() ?

 

Any updates

Reason: