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

 
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.

Show your updated code please.
 

Aparently, I solved the problema using the operator "&" in the function Compare() to get the value of "this":


int PriceScore::Compare(const CObject *node,const int mode=0) const
{
   PriceScore *thisObject = (PriceScore*)&this;
   PriceScore *pc = (PriceScore*)node;

   Print(__FUNCTION__,":Compare called. Incoming: ", pc.Score()," This: ", score);
   
   if(pc.Score() < thisObject.getPrice())
      return 1;
   else if(pc.Score() > thisObject.getPrice())
      return -1;
   else
      return 0;
}


Finally I got the value I was expecting.

 

You can drop thisObject and replace it by "this" in the comparison.

But, why dont you use the "operator" functionality instead of a function "Compare"? Makes the code easier to read later.

 
Doerk Hilger:

You can drop thisObject and replace it by "this" in the comparison.

But, why dont you use the "operator" functionality instead of a function "Compare"? Makes the code easier to read later.


Because it's a polymorphic method of the base class CObject and it has to be overridden in order to implement sorting and searching in the Collection classes, CArrayObj and CList.

 
Gustavo Hennemann:

Aparently, I solved the problema using the operator "&" in the function Compare() to get the value of "this":



Finally I got the value I was expecting.


I'm not sure what you hope to achieve with this logic. Typically you would want to only compare the same elements for sorting and searching. 

...
if(this.thingToSort > other.thingToSort) return 1;
...
 
nicholishen:

I'm not sure what you hope to achieve with this logic. Typically you would want to only compare the same elements for sorting and searching. 

Exactly, now it is sorting, before that I could not make the comparison, I was getting erros and errors and errors. Now it's working.

This approach is not obvious and is not in any document, I had to do a lot of tests and "trial and error" until to get this. I hope others can enjoy the solution.

 
Gustavo Hennemann:

Exactly, now it is sorting, before that I could not make the comparison, I was getting erros and errors and errors. Now it's working.

This approach is not obvious and is not in any document, I had to do a lot of tests and "trial and error" until to get this. I hope others can enjoy the solution.


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


int PriceScore::Compare(const CObject *node,const int mode=0) const
{
   /* PriceScore *thisObject = (PriceScore*)&this; */
   PriceScore *pc = (PriceScore*)node;

   Print(__FUNCTION__,":Compare called. Incoming: ", pc.Score()," This: ", score);
   
   if(pc.Score() < /*thisObject.*/getPrice())
      return 1;
   else if(pc.Score() > /*thisObject.*/getPrice())
      return -1;
   else
      return 0;
}
 
Ex Ovo Omnia:

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


It's the equivalent of using GetPointer(this)
 
nicholishen:
It's the equivalent of using GetPointer(this)

Well, that does not answer the question "why". There was no reason to reveal the pointer to self.

 
Ex Ovo Omnia:

Well, that does not answer the question "why". There was no reason to reveal the pointer to self.

No, definitely not in this example since it's a redundant extraction, but let's say you were passing this to a method that only accepted a pointer then you would need to use it... method(&this)
Reason: