【发布时间】:2019-08-21 23:54:29
【问题描述】:
#include <iostream>
using namespace std;
class B{
int temp1;
public :
void disp()
{
cout <<temp1;
}
void setA(int a)
{
temp1=a;
}
void operator ++(int)
{
temp1+=5;
}
void cal()
{
temp1 ++;
}
};
int main()
{
B b;
b.setA(10);
b.cal();
b.disp();
return 0;
}
我最近了解了运算符重载,所以在玩代码......所以这里的预期答案是 15,但它显示为 11。为什么我的重载运算符不起作用......特别是这有什么问题代码 rs,因为这部分似乎存在逻辑错误:
void operator ++(int)
{
temp1+=5;
}
void cal()
{
temp1 ++;
}
【问题讨论】:
-
temp1是一个整数,而不是B。 -
您希望在这里发生什么:
int temp1=10; temp1++;。那么,为什么你的班级的结果会有所不同呢?附言这并不是后增量重载应该如何工作的,但这是另一个问题。
标签: c++ operator-overloading post-increment