About Store Forum Documentation Contact



Post Reply 
Mem Simple - sorting
Author Message
Azriel Offline
Member

Post: #1
Mem Simple - sorting
I've encountered a problem (well, lack of knowledge) with sorting Mems.
Code:
class A
{
public:
    int x;
  
    int getX()
    {
        return x;
    }
};

Mems<A> container;

Edit:
I figured out how to sort it, but there's 1 more thing I'd like to know.
I want the container to sort by x variable, so I call container.sort(customSort).
That's how it looks like:
Code:
Int customSort(C A &a,  C A &b)
{
   if(a.x<b.x)
      return 1;
   else
      return -1;
}

It works fine, but if I use a.getX() instead of a.x, I get an error:
Code:
1>Source\Main.cpp(153): error C2662: 'I32 A::getX(void)' : cannot convert 'this' pointer from 'const A' to 'A &'
1>          Conversion loses qualifiers
What's the problem here?

Edit2:
Ok, solved it.
Int customSort(C A &a, C A &b) takes arguments as consts, so I couldn't use A class non-const functions in it. The solution was to either const_cast object a in customSort function or to make the getX() function const (which is better I guess).

Edit3:
How can I make it work with container of pointers?
(This post was last modified: 10-28-2015 04:43 PM by Azriel.)
10-28-2015 03:40 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Mem Simple - sorting
You need to make the pointer const, not the actual data const.
like that:
static Int CompareName(Symbol*C &a, Symbol*C &b) {..}

Your Int customSort(C A &a, C A &b)
should return 0, when the elements are equal.
10-28-2015 10:54 PM
Find all posts by this user Quote this message in a reply
Azriel Offline
Member

Post: #3
RE: Mem Simple - sorting
Works now, thanks.
10-29-2015 12:36 PM
Find all posts by this user Quote this message in a reply
Post Reply