0

I have a class like

class Date{
 public:
  Date(int year, int month, int date);
  Date(const Date &d);
  Date &operator=(const Date &d);

  bool isHoliday();
  void addDays(int days);
}

In my addDays() implementation, I need to make a call to isHoliday(). I have the following two options, both of which seem to be allowed by the compiler.

Option 1:

void Date::addDays(int days) {
 if (this->isHoliday()) {
  // body
 }
}

Option 2:

void Date::addDays(int days) {
 if (isHoliday()) {
  // body
 }
}

Is there any difference between the two definitions? Which is the correct one to use? Thanks.

EDIT: The referenced duplicate question indeed answers my question. Just couldn't find it as I was searching by ->. I can't delete this now, but do refer to that question for a good number of answers.

kovac
  • 4,945
  • 9
  • 47
  • 90

1 Answers1

2

There's no difference here. isHoliday() calls the method on the this object already. so explicitly saying this->isHoliday() isn't required and does the same thing.

In that regard both are "correct to use" as it comes down on personal preference. Around where I work we use isHoliday() instead of this->isHoliday() as it reduces code clutter but this isn't the de facto standard.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122