-3

Why does the code below change the value of D1 though I have not used assignment operator?

#include <iostream>
using namespace std;

class Distance {
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12

   public:
      // required constructors
      Distance() {
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i) {
         feet = f;
         inches = i;
      }

      // method to display distance
      void displayDistance() {
         cout << "F: " << feet << " I:" << inches <<endl;
      }

      // overloaded minus (-) operator
      Distance operator- () {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
};

int main() {
   Distance D1(1, 10), D2(-5, 11);

   -D1;                     // apply negation
   D1.displayDistance();    // display D1

   -D2;                     // apply negation
   D2.displayDistance();    // display D2

   return 0;
}

The output is

F: -1 I:-10
F: 5 I:-11

Michelle
  • 97
  • 1
  • 10
  • 3
    *"though I have not used assignment operator?"* You use it twice in `operator-`. `feet = -feet;` and `inches = -inches;`. – François Andrieux Sep 05 '18 at 17:52
  • @πάνταῥεῖ I'm not sure it's a duplicate. The question isn't asking how to override `operator-`. It's asking about why the code is behaving the way it does. – François Andrieux Sep 05 '18 at 17:54
  • @Françoise Feel free to reopen. – πάντα ῥεῖ Sep 05 '18 at 17:55
  • @FrançoisAndrieux thank you, I don't know how I missed it. I was focusing on the return statement. Silly me. – Michelle Sep 05 '18 at 17:56
  • 4
    @Michelle If a member function (including operators) isn't intended to modify it's instance, make is `const` so the compiler can signal these kinds of mistakes. Ex. `Distance operator-() const`. – François Andrieux Sep 05 '18 at 17:56
  • @FrançoisAndrieux yes I will surely keep that in mind, thank you. – Michelle Sep 05 '18 at 17:57
  • 1
    The dupe is dead, but [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) is still a highly recommended read and bookmark. Much wisdom contained therein. – user4581301 Sep 05 '18 at 18:08

1 Answers1

0

If you don't want a specific function to change your class variables, you have to use const word right after it's declaration. So when ever you will use assignment sentences inside this function, the compiler will throw an error to you. The following lines are change the current object you are working on:

feet = -feet;
inches = -inches;

When you use const, the following function will throw you a compiler time exception:

Distance operator- () const {
    feet = -feet; // Compiler exception..
    inches = -inches; // Compiler exception..
    return Distance(-feet, -inches);
}

The following code, as I understood you, do what you want:

Distance operator- () const {
    return Distance(-feet, -inches);
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22