5

I am still new to C++ (programming in general)and forgive me if this question is stupid or has been asked numerously. Here is the question..Let's say there are two objects A and B under the same class.

e.g

  class Fruit{
  int apple;
  int banana;
      fruit(int x, int y){
       apple=x;
       banana=y;
      }
  }
  Fruit A(1,1);
  Fruit B(1,1);

If I want to check if content from Object A is the same as Object B's, do I have to compare every variablefrom A to B, or

   if(Object A == Object B)
   return true;

will do the job?

GalaxyVintage
  • 656
  • 1
  • 11
  • 20
  • How could `if(Object A == Object B) return true;` ever do the job when it has zero chance of compiling? You need to implement `operator==` and compare whatever data members you want for equality. – Praetorian May 15 '15 at 00:40
  • possible duplicate of [Why don't C++ compilers define operator== and operator!=?](http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator) – Denilson Amorim May 15 '15 at 00:41
  • Not quite exactly a dupe but yes, IMO it should be flagged as one since there's already a very good database of explanations in the linked due question... – Denilson Amorim May 15 '15 at 00:42
  • @thelink2012 Frankly I cannot think of any good reason why the compiler decides to implement a default copy ctor (even for classes with pointers) but is stubborn to generate a default `operator==`. Can you? – vsoftco May 15 '15 at 00:46
  • @vsoftco Michael Burr's [answer](https://stackoverflow.com/a/218713/241631) to the linked question explains the reason quite well, oddly the OP chose an answer that doesn't give nearly as good a reason. Also there's a proposal making the rounds for allowing `= default` for the comparison operators. – Praetorian May 15 '15 at 01:00
  • @Praetorian I liked that answer, thanks for the link. – vsoftco May 15 '15 at 01:05

1 Answers1

9
if(Object A == Object B)
    return true;

will do the job? No it won't, it won't even compile

error: no match for 'operator==' (operand types are 'Fruit' and 'Fruit')

You need to implement a comparison operator==, like

bool Fruit::operator==(const Fruit& rhs) const
{
    return (apple == rhs.apple) && (banana == rhs.banana);
    // or, in C++11 (must #include <tuple>) 
    // return std::tie(apple, banana) == std::tie(rhs.apple, rhs.banana); 
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Sorry I don't quite get how this operator works...Can you explain a bit more ? – GalaxyVintage May 15 '15 at 00:42
  • 3
    @Galaxyvintage When you write `fruit1==fruit2`, the compiler translates it to `fruit1.operator==(fruit2)`. So `operator==` is invoked on the left hand side instance like any other function. The rest I think it's clear, inside you are just comparing field by field. – vsoftco May 15 '15 at 00:44
  • I add the nit that the operator itself should be const. – user3344003 May 15 '15 at 00:54
  • @vsoftco No, of course it doesn't mean that. It should be `bool Fruit::operator==(const Fruit& rhs) const` and the last `const` means the object on which you're invoking `operator==` will not be modified by the function. – Praetorian May 15 '15 at 01:05
  • @Praetorian I think it's too late for me, of course you're right, I don't know what I was thinking of. You can of course call `const` functions from a non-const instance, but not the other way around. – vsoftco May 15 '15 at 01:09
  • @user3344003 yes, indeed – vsoftco May 15 '15 at 01:09
  • 3
    A C++14 trick to avoid all that `std::tie` boilerplate - define another member function `auto members() const { return std::tie(...); }` Then the body of the above operator becomes `return members() == rhs.members();` – Praetorian May 15 '15 at 01:14
  • @Praetorian Neat! I wonder whether there's any quick way of making it work in C++11, without a nasty `decltype(std::tie(...))` trailing return type. – vsoftco May 15 '15 at 01:17
  • @vsoftco for the `const Fruit& rhs` part, why does it have to be constant? – GalaxyVintage May 15 '15 at 01:34
  • Because if won't allow you to modify `rhs` by mistake inside `operator==` (for example, indirectly via calling a non-`const` member). It enforces `const` usage and makes your code safer. – vsoftco May 15 '15 at 01:39
  • Does that mean rhs has to be a const when it's created? or the function simply treats it as a const? – GalaxyVintage May 15 '15 at 01:49
  • @Galaxyvintage no, it doesn't have to. It is treated as if it had been a constant from the beginning (technically the local parameter `rhs` is a `const Fruit&` reference to your original object you pass to `operator==`). Passing by value also doesn't let you change the original object (as you access a copy of it), however it can be more expensive due to the compiler performing a copy, so passing by reference is preferred. With C++11, passing by value became OK in some instances, due to move semantics, by which objects can be *moved* instead of *copied*. – vsoftco May 15 '15 at 01:53