方式一:
#include <iostream>
using namespace std;
struct Number
{
public:
Number(int a, int b, int c)
: first(a), second(b), third(c)
{
}
Number(const Number & rhs)
: first(rhs.first), second(rhs.second), third(rhs.third)
{
}
bool operator < (const Number & rhs) const
{
if (first != rhs.first) {
return (first < rhs.first);
}
if (second != rhs.second) {
return (second < rhs.second);
}
if (third != rhs.third) {
return (third < rhs.third);
}
return false;
}
bool operator > (const Number & rhs) const
{
return (rhs < *this);
}
bool operator <= (const Number & rhs) const
{
return (!(*this > rhs));
}
bool operator >= (const Number & rhs) const
{
return (!(*this < rhs));
}
bool operator != (const Number & rhs) const
{
return (*this < rhs || *this > rhs); // 或者写原始比较的代码
}
bool operator == (const Number & rhs) const
{
return (!(*this != rhs));
}
friend ostream & operator << (ostream & os, const Number & num);
private:
const int first;
const int second;
const int third;
};
ostream & operator << (ostream & os, const Number & num)
{
return (os << "{" << num.first << ", "
<< num.second << ", " << num.third << "}");
}
int main()
{
Number lhs(1, 2, 5);
Number rhs(1, 3, 4);
cout << "lhs : " << lhs << endl;
cout << "rhs : " << rhs << endl;
cout << " < ? " << (lhs < rhs) << endl;
cout << " > ? " << (lhs > rhs) << endl;
cout << " <= ? " << (lhs <= rhs) << endl;
cout << " >= ? " << (lhs >= rhs) << endl;
cout << " == ? " << (lhs == rhs) << endl;
cout << " != ? " << (lhs != rhs) << endl;
return 0;
}
方式二:
#include <iostream>
using namespace std;
struct Number
{
public:
Number(int a, int b, int c)
: first(a), second(b), third(c)
{
}
Number(const Number & rhs)
: first(rhs.first), second(rhs.second), third(rhs.third)
{
}
bool operator < (const Number & rhs) const
{
return (compare(rhs) < 0);
}
bool operator > (const Number & rhs) const
{
return (compare(rhs) > 0);
}
bool operator <= (const Number & rhs) const
{
return (compare(rhs) <= 0);
}
bool operator >= (const Number & rhs) const
{
return (compare(rhs) >= 0);
}
bool operator != (const Number & rhs) const
{
return (compare(rhs) != 0);
}
bool operator == (const Number & rhs) const
{
return (compare(rhs) == 0);
}
friend ostream & operator << (ostream & os, const Number & num);
private:
int compare(const Number & rhs) const
{
if (first < rhs.first) {
return -1;
}
else if (first > rhs.first) {
return 1;
}
if (second < rhs.second) {
return -1;
}
else if (second > rhs.second) {
return 1;
}
if (third < rhs.third) {
return -1;
}
else if (third > rhs.third) {
return 1;
}
else {
return 0;
}
}
private:
const int first;
const int second;
const int third;
};
ostream & operator << (ostream & os, const Number & num)
{
return (os << "{" << num.first << ", "
<< num.second << ", " << num.third << "}");
}
int main()
{
Number lhs(1, 2, 5);
Number rhs(1, 3, 4);
cout << "lhs : " << lhs << endl;
cout << "rhs : " << rhs << endl;
cout << " < ? " << (lhs < rhs) << endl;
cout << " > ? " << (lhs > rhs) << endl;
cout << " <= ? " << (lhs <= rhs) << endl;
cout << " >= ? " << (lhs >= rhs) << endl;
cout << " == ? " << (lhs == rhs) << endl;
cout << " != ? " << (lhs != rhs) << endl;
return 0;
}