简体   繁体   English

-减去C ++中的一元运算符重载

[英]- 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. 我是初学者,学习C ++重载运算符函数。 This code is actually working fine but at one step I am little confused about // return Distance(feet, inches); 这段代码实际上可以正常工作,但是第一步我对// return Distance(feet, inches);感到困惑// 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. 影响-D1之类的表达式的值,因此,如果执行D1 = -D2 ,则使用return会有所不同。

  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. 但是,您需要阅读很多有关汇编,编译器和cpu的知识,才能更好地理解它。

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] : 来自[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. 从构造函数,析构函数或具有cv void返回类型的函数的末尾流出就等于没有操作数的返回。 Otherwise, flowing off the end of a function other than main ([basic.start.main]) results in undefined behavior. 否则,从main([basic.start.main])以外的函数的末尾流出会导致未定义的行为。

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 . 那条线返回一个以feetinches为单位的Distanceprvalue That value should be eligible to copy-elision and is guaranteed to be elided in copy from C++17 该值应该可以进行复制删除,并且可以保证从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: 从上述的[stmt.return]中,重点是我的:

The expr-or-braced-init-list of a return statement is called its operand. return语句的expr-or-braced-init-list称为其操作数。 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]). 没有操作数的return语句只能在返回类型为cv void,构造函数([class.ctor])或析构函数([class.dtor])的函数中使用。 A return statement with an operand of type void shall be used only in a function whose return type is cv void. 操作数类型为void的return语句只能在返回类型为cv void的函数中使用。 A return statement with any other operand shall be used only in a function whose return type is not cv void; 带有任何其他操作数的return语句只能在返回类型不是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 . return语句通过从操作数进行复制初始化([dcl.init])来初始化(显式或隐式)函数调用的glvalue结果或prvalue结果对象 [ 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. [注意:如果return语句不是prvalue或类型与函数的返回类型不同,则返回语句可以涉及构造函数的调用以执行操作数的复制或移动。 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]). 如果返回了自动存储持续时间变量([class.copy]),则可以取消与return语句关联的复制操作或将其转换为move操作。 — 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语句应采用以下形式:

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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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