2

I was watching this seminar on C++ best practices, and the speaker gave this code example:

struct Data{
  int x;
  int y;

  bool operator==(Data &rhs){
   return x == rhs.x && y == rhs.y;
}
};

He then asked what was missing in this code. As a newbie I thought that nothing was missing, but then he pointed out that 2 const keywords were missing, like so:

struct Data{
  int x;
  int y;

    bool operator==(const Data &rhs) const{
     return x == rhs.x && y == rhs.y;
  }
};

Now I think this is like a promise not to modify the object. But can someone explain why these const keywords are necessary?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dany
  • 37
  • 5
  • 5
    I would expect that this "seminar on c++ best practices" will also explain why this is mandatory. After well, what the point of having a "seminar on c++ best practices" when it doesn't explain the reason for these best practices? – Sam Varshavchik Jan 10 '23 at 19:11
  • @SamVarshavchik He did not explain. Here is the link to the video if you are interesed. https://www.youtube.com/watch?v=nqfgOCU_Do4 – Dany Jan 10 '23 at 19:14
  • 1
    Here's the problem: any clown can upload a video to Youtube, even I can do that. Although this is true, the fact that you even have to ask this question only goes to show that Youtube is not a replacement for a C++ textbook, and a good, edited textbook is required in order to learn and fully understand core C++ fundamentals. It is not realistic to expect to be able to thoroughly learn C++ from reading blogs and watching Youtube. – Sam Varshavchik Jan 10 '23 at 19:17
  • 3
    fyi (not an exact duplicate) [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). Also [Comparison operators](https://en.cppreference.com/w/cpp/language/operator_comparison) - typical/expected signatures. – Richard Critten Jan 10 '23 at 19:19
  • There is nothing special about `operator==` in this context. The reasons for using `const` here are the same reasons you would have for any function or method that you write. – john Jan 10 '23 at 19:54
  • As the 'basic rules' link above explains, it is generally thought that `operator==` should be implemented as a non-member function. This is best practice, so that doesn't say much for the best practice video you are watching. – john Jan 10 '23 at 19:59
  • I'd also consider to add `[[nodiscard]]` – MatG Jan 10 '23 at 20:00

1 Answers1

6

If you did not have const on both places in your operator==, this would not compile:

void foo(const Data& lhs, const Data& rhs) {
    if(lhs == rhs) { // requires `operator==(const Data &rhs) const`
       // do stuff
    }
}

why is this mandatory

It is not - but it's good practice - and failing to implement it that way will severely inhibit the ability to interact with the standard (and other) libraries.

  1. Your aim is not to change the value of the parameter you take by reference. If that parameter is const and you take it by a const& you will therefore still be able to read from it in your function. Had your declaration only said Data& rhs, it would not compile.

  2. Your aim is not to change the state of *this (the lefthand side of the == operator). The same applies here. If *this is const, the const qualifier on the member function makes it still possible to use the member function.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108