简体   繁体   中英

- minus unary operator overloading in c++

#include <iostream>
#include<conio.h>
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(11, 10), D2(-5, 11);

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

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

   return 0;
}

I am a beginner learning C++ overloading operator function. This code is actually working fine but at one step I am little confused about // return Distance(feet, inches); I have made this statement as comment in program but still output is true. but if i run program without making it comment the program also works fine than in what purpose is this statement is using? Second, is it constructor function returning values? Third, how it is returning values I mean it is not a variable i always heard we can return values from variable?

  1. return Distance(feet, inches); affects the value of expressions like -D1 , so if you do D1 = -D2 , then using return will make a difference.

  2. A constructor function does not return anything. It just contains the code that runs when an object is created.

  3. In some sense, the return value is like an invisible variable. However, you would need to read a lot about assembly, compilers, and the cpu to understand it better.

if I run program without making it comment the program also works fine than in what purpose is this statement is using

From [stmt.return] :

Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main ([basic.start.main]) results in undefined behavior.

Your program is resulting an undefined behavior , anything can happen.

Second, is it constructor function returning values?

I think you meant this line:

return Distance(feet, inches);

That line return a prvalue of Distance which is constructed from feet and inches . That value should be eligible to copy-elision and is guaranteed to be elided in copy from C++17

Third, how it is returning values I mean it is not a variable I always heard we can return values from variable?

From the said [stmt.return], emphasis is mine:

The expr-or-braced-init-list of a return statement is called its operand. A return statement with no operand shall be used only in a function whose return type is cv void, a constructor ([class.ctor]), or a destructor ([class.dtor]). A return statement with an operand of type void shall be used only in a function whose return type is cv void. A return statement with any other operand shall be used only in a function whose return type is not cv void; the return statement initializes the glvalue result or prvalue result object of the (explicit or implicit) function call by copy-initialization ([dcl.init]) from the operand . [ Note: A return statement can involve an invocation of a constructor to perform a copy or move of the operand if it is not a prvalue or if its type differs from the return type of the function. A copy operation associated with a return statement may be elided or converted to a move operation if an automatic storage duration variable is returned ([class.copy]). — end note ] [ Example:

  std::pair<std::string,int> f(const char* p, int x) { return {p,x}; } 

The return statement should go in those form:

return; // for void and constructor, destructor

or

return expression-or-braced-init-list;

Anyway, I think your minus operator should look like this:

 Distance operator- () {
    return Distance(-feet, -inches);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM