If a class has overloaded an operator to facilitate sorting its objects by a particular attribute, is there a way to overload the operator again to sort by another attribute? For example, the class below has overloaded the less than < operator to compare its data member minutes, is there a way to do the same for its data member hours or would I just be better off creating a binary predicate for each sorting criteria? Thanks in advance.
class PhoneCall {
friend ostream& operator<<( ostream&,const PhoneCall& );
private:
int minutes;
int hours;
public:
PhoneCall(int = 0);
bool operator<(const PhoneCall&);
};
ostream& operator<<(ostream& out, const PhoneCall& p) {
out << "Phone call lasted " << p.minutes << " minutes" << endl;
return out;
}
PhoneCall::PhoneCall(int ct) {
minutes = ct;
}
bool PhoneCall::operator<(const PhoneCall& p) {
bool less = (minutes < p.minutes)? true: false;
return less;
}
Aucun commentaire:
Enregistrer un commentaire