如何对一元后缀运算符应用运算符重载

发布于 2025-01-11 00:28:09 字数 1141 浏览 2 评论 0原文

下面是一元运算符 ++ 的运算符重载代码,

#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+1;
         inches = inches+1;
         
         return Distance(feet, inches);
      }
};

int main() {
   Distance D1(11, 10), D2(-5, 11);
 
   ++D1;                     // increment by 1
   
   D1.displayDistance();    // display D1

   ++D2;                     // increment by 1
   D2.displayDistance();    // display D2

   return 0;
}

当我使用上面的代码时,我可以成功使用前缀运算符 ++D1 和 ++D2 但我不知道如何重载后缀运算符 D1++ 和 D2++ 即使我在上面的代码中尝试这些,它也会显示错误 那么我们如何分别对后缀和前缀使用运算符重载的概念呢?

below is code for operator overloading for unary 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+1;
         inches = inches+1;
         
         return Distance(feet, inches);
      }
};

int main() {
   Distance D1(11, 10), D2(-5, 11);
 
   ++D1;                     // increment by 1
   
   D1.displayDistance();    // display D1

   ++D2;                     // increment by 1
   D2.displayDistance();    // display D2

   return 0;
}

when I use above code then I can successfully use prefix operator ++D1 and ++D2
but I am not getting how to overload postfix operator D1++ and D2++
even if I try these in above code it showing me error
so how can we use concept of operator overloading for postfix and prefix separately?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2025-01-18 00:28:09

对于后缀operator++,您必须指定一个int类型的额外(未使用)参数,如下所示:

class Distance {
   
      //other code as before
      public:
      Distance operator++(int);//declaration for postfix operator++
};

//other code as before 

//definition for postfix operator++
Distance Distance::operator++(int)
{    
    Distance ret = *this;   // save the current value
    
    ++*this;     // use prefix ++
    return ret;  // return the saved state
}

请参阅演示

说明

在定义前缀和后缀运算符时存在问题,因为这两个版本使用相同的符号,这意味着这些运算符的重载版本具有相同的名称。而且,它们还具有相同数量和类型的操作数。

因此,为了解决这个问题,postfix版本需要一个int类型的额外参数。当我们使用后缀运算符时,编译器会自动/隐式提供 0 作为此参数的参数。

For postfix operator++ you have to specify an extra(unused) parameter of type int as shown below:

class Distance {
   
      //other code as before
      public:
      Distance operator++(int);//declaration for postfix operator++
};

//other code as before 

//definition for postfix operator++
Distance Distance::operator++(int)
{    
    Distance ret = *this;   // save the current value
    
    ++*this;     // use prefix ++
    return ret;  // return the saved state
}

See DEMO.

Explanation

There is a problem when defining both the prefix and postfix operators because both of these versions use the same symbols, meaning that the overloaded versions of these operators have the same name. Moreover, they also have the same number and type of operands.

So to solve this problem, the postfix version take an extra parameter of type int. And when we use the postfix operator, the compiler automatically/implicitly supplies 0 as the argument for this parameter.

行雁书 2025-01-18 00:28:09

如果你想要 post-inc/dec 那么代码将是:

  Distance operator++ (int) {
     feet = feet+1;
     inches = inches+1;
     
     return Distance(feet, inches);
  }

我们在形式参数中使用 int 。只是修复后/修复前的情况不同。
运算符的前缀形式的声明方式与任何其他一元运算符完全相同;后缀形式接受 int 类型的额外参数。

if you want post-inc/dec then the code will be :

  Distance operator++ (int) {
     feet = feet+1;
     inches = inches+1;
     
     return Distance(feet, inches);
  }

we use int in formal parameter . it is just crate different between post/pre-fix.
The prefix form of the operator is declared exactly the same way as any other unary operator; the postfix form accepts an extra argument of type int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文