Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,395 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,128 people online right now. Registration is fast and FREE... Join Now!




Overloaded Operators and List.Sort()

 
Reply to this topicStart new topic

Overloaded Operators and List.Sort(), Not sure what im doing wrong.

Vextor
28 Sep, 2006 - 08:21 PM
Post #1

D.I.C Regular
Group Icon

Joined: 22 May, 2002
Posts: 288



Thanked: 1 times
Dream Kudos: 25
My Contributions
Once again I'm stuck and i'm certain it's something i'm overlooking. I'm working on a project for my class that reads in a text file, builds a list based of those elements and then we're supposed to be able to manipulate that list. Everythign works except the List.Sort(). Here's the way I understand it.


Right now I'm building a list of pointers to my class objects. Inside my class i've overloaded <, >, and = to work for my objects. However, the list will not sort for me at all. I have a feeling it has something to do with the fact that the sort function works on the pointers themselves rather then the data. Here's my first question.

When you overload >, for instance and you're comparing say 2 class objects how can you get it to recieve a pointer on the left? Logically this doesn't seem to work....

CODE

bool SomeClass::Operator <(SomeClass *elem)
{
        if(Data > elem->Data)
          return true;
        else
          return false;
}


So say we have a List populated with n SomeClass *'s. Would the above operator overload work for a comparison or would I have to dereference that data somehow?
User is offlineProfile CardPM
+Quote Post

Vextor
RE: Overloaded Operators And List.Sort()
29 Sep, 2006 - 12:53 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 May, 2002
Posts: 288



Thanked: 1 times
Dream Kudos: 25
My Contributions
After working on it a bit last night, googling lists, sorting, and pointers and every combination there of I was able to get it to work. However, im not sure I understand what is going on so if somone can explain it I would appreciate it. I want to both be able to understand and implement it.

Within my class that manages (builds, populates and manipulates) my list object I built a boolean function object (I think), that is later passed into the list.sort() function. Here's what I added...


CODE

struct SortProcess : public std::binary_function<LineObject*, LineObject*, bool>
{
    bool operator()(LineObject* left, LineObject *right) const
    {
        if(left->get_carValue() < right->get_carValue())
        return true;

        else if(left->get_carValue() == right->get_carValue())
        {
            if(left->get_driverName() < right->get_driverName())
                return true;
            else
                return false;
        }

        else
        return false;
    }
};



Then later, when I called list.sort all I had to do was pass it SortProcess
CODE

.
.
mylist.sort(SortProcess);
.
.


I hope im explaining it right....
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month