How to compare two objects (the calling object and the parameter) in a class?

微笑、不失礼 提交于 2019-11-28 10:20:22
int Date :: Compare (const Date& d) {

   if (year<d.year) {
      return -1;
   }
   else if (year>d.year) {
      return 1;
   }
   else if (month<d.month) {
      return -1;
   }
   else if (month>d.month) {
      return 1;
   }
   // same for day

   return 0;
}

Usually, you'lll also want to provide overloaded comparison operators, for example (also within the class definition):

bool operator == (const Date& d) const {
   return !Compare(d);
}

bool operator < (const Date& d) const {
  return Compare(d)<0;   
}

... // consider using boost::operators

PS: There are smarter implementations of Compare() - just check the other answers. This one is pretty straightforward and readable, but conforms exactly to your specification.

Here's how I might implement your Compare function, although the format takes a moment to get used to:

int Date::Compare(const Date& d) const {
  return
    (year < d.year)   ? -1 :
    (year > d.year)   ?  1 :
    (month < d.month) ? -1 :
    (month > d.month) ?  1 :
    (day < d.day)     ? -1 :
    (day > d.day)     ?  1 :
                         0;
}

Or perhaps:

template<typename T>
int Compare(T a, T b) {
    if (a < b) return -1;
    if (b < a) return 1;
    return 0;
}

int Date::Compare(const Date& d) const {
    int a = Compare(year, d.year);
    if (a == 0) a = Compare(month, d.month);
    if (a == 0) a = Compare(day, d.day);
    return a;
}

I wouldn't use operator== in Compare, although the answers telling you how to implement operator== are fine if you want that as well. The reason is that operator== is clearly going to have to look at the same fields compare does, and if it returns false then Compare will do very similar work again. Efficiency probably isn't an issue, but it duplicates the logic.

And for what it's worth, idiomatic C++ is to implement operator< and possibly also a consistent operator== and operator>, rather than an all-in-one Compare function. The operators are what the standard algorithms use for searching and sorting, and everything else follows. Java chose to do things differently.

into the class's public area

bool operator==(const Date& rhs) const {
    return
       year == rhs.year
       && month == rhs.month
       && day == rhs.day
    ;
}

Compare object by contents, i.e. in your case the dates are equal of the day, month and year are equal (and perhaps format - depending on your semantics).

Also, C++ already includes a great facility for object comparison: operator == which allows writing clearer code than calling a Compare method.

By the way, take care with this:

  if (d1 == d2)
     cout << "The dates are the same";
     return (0);

If the condition is true, the cout line will be executed. The return will be executed even if the condition is false.

The semantics of C++'s || make this a little cluttered:

static inline int cmp(int a, int b)
{
  return a < b ? -1 : a == b ? 0 : 1;
}

int Date::Compare(const Date& d)
{
  int result;
  (result = cmp(year, d.year))     ||
    (result = cmp(month, d.month)) ||
      (result = cmp(day, d.day));

  return result;
}

In order to use operator== for user-defined types, you must implement it. In addition, your Compare function should be marked as a const member function:

class Date
{
...
int Compare(const Date& d) const;     

bool operator==(const Date& rhs) const
{
    return 0 == Compare(rhs);
}

You can't do d1 === d2, because I believe it compares the memory addresses (haven't done C++ in a while).

What you need to do is write a function that will compare each member of your Date class and return negative number, 0, or positive number. Negative means lesser, 0 means the same, and positive means greater.

For example, in Java:

public int compareTo(Date date) {
  int returnValue = 0;

   returnValue = this.getYear() - date.getYear();

   if(returnValue == 0) {
      returnValue = this.getMonth() - date.getMonth();

      if(returnValue == 0) {
         returnValue = this.getDay() - date.getDay();
      }
   }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!