如何对一元后缀运算符应用运算符重载
下面是一元运算符 ++ 的运算符重载代码,
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于后缀
operator++
,您必须指定一个int
类型的额外(未使用)参数,如下所示:请参阅演示。
说明
在定义前缀和后缀运算符时存在问题,因为这两个版本使用相同的符号,这意味着这些运算符的重载版本具有相同的名称。而且,它们还具有相同数量和类型的操作数。
因此,为了解决这个问题,postfix版本需要一个
int
类型的额外参数。当我们使用后缀运算符时,编译器会自动/隐式提供0
作为此参数的参数。For postfix
operator++
you have to specify an extra(unused) parameter of typeint
as shown below: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 supplies0
as the argument for this parameter.如果你想要 post-inc/dec 那么代码将是:
我们在形式参数中使用
int
。只是修复后/修复前的情况不同。运算符的前缀形式的声明方式与任何其他一元运算符完全相同;后缀形式接受 int 类型的额外参数。
if you want post-inc/dec then the code will be :
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.