![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
In C++ you can assign custom operators to classes. For instance, lets say I have a class Pet, and I consider two Pets as being equal if they have the same name and the same species. I can go about this one of two ways. I could have a function on the class like so:
C++ Syntax (Toggle Plain Text)
C++ Syntax (Toggle Plain Text)
C++ Syntax (Toggle Plain Text)
C++ Syntax (Toggle Plain Text)
So all you need do is override the less than operator, and sort your container class with std::sort. For instance, if you have a vector<Customer> object called customers: C++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#22 | |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
I'm pretty familiar with operator overloading, my question is where in the sort function syntax is there a greater than or lesser than operator. Could you explain the use of the sort function with these operators inside the function call. Or could you give me a link to a site that explains the use of the sort function with respect to these operators.
Quote:
|
|
|
|
|
|
|
#23 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
The operator is called within the sort function itself.
|
|
|
|
|
|
#24 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
So you just overload the < operator as member function for the Customer class? And due to the implementation of the sort function, where ever it is located, will recognize to use the overloaded operator since it is comparing 2 customer objects. Correct?
|
|
|
|
|
|
#25 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
You got it.
|
|
|
|
|
|
#26 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Using your implementation a few posts above, why did you pass a reference to a customer object. I see that you are using this, so how does the sort function know the invoking object? Does the object on the left side invoke the overloaded function, thus requires accepting the other one as an argument? Does that make sense? Also, to take into account sorting by first name, if the last names are equal, would it make sense to use some conditionals to choose wha treturn to make so if the last names are equal, you would return one as less than the other.
e.g. - if( (customer.lastName == this->lastName) && customer.firstName < this->firstName)
return (customer.lastName < this->lastName);
else if( (customer.lastName == this->lastName) && customer.firstName) > this->firstName)
return (this->lastName < customer.lastName);
else
return ( customer.lastName < this->lastName);
}Would something like this work in this situation? |
|
|
|
|
|
#27 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
C++ Syntax (Toggle Plain Text)
Here's the correct boolean logic: C++ Syntax (Toggle Plain Text)
|
||
|
|
|
|
|
#28 |
|
Unverified User
Join Date: Sep 2005
Posts: 209
Rep Power: 0
![]() |
Haha, well at least you understood me! I'm probably going to start coding this tonight or tomorrow, so be on the lookout for actual source. I'm thinking of using a large MegaStore class, as you stated before to handle all of the queues inside the store, and manage statistics for each line. Sound good? Looks like that will be the easiest way to make function calls and what not and not have to worry about what object is invoking what and where/if I need to pass this object so it can be used to process this, etc...
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Game programming forum | msk420 | Community Announcements and Feedback | 18 | Jun 6th, 2006 8:47 PM |
| Game Programming | ReggaetonKing | Coder's Corner Lounge | 9 | Apr 25th, 2006 4:59 PM |
| Python programming game? | commodore | Python | 7 | Feb 7th, 2006 6:26 PM |
| game programming | gammaman | C++ | 17 | Oct 23rd, 2005 2:42 AM |
| Java programmers, game developers, artists, be ware! RPG game team is recruiting! | atcomputers.us | Paid Job Offers | 7 | Sep 25th, 2005 7:25 PM |