重载postfix运算符不起作用 [英] Overloading postfix operator doesn't work
问题描述
#include <iostream>
using namespace std;
class NumDays
{
private:
int hour;
int day;
void simplify();
public:
NumDays()
{
day = 0;
hour = 0;
}
void setData(int d, int h)
{
hour = h;
day = d;
simplify();
}
int getHour()
{
return hour;
}
int getDay()
{
return day;
}
NumDays operator++(int);
NumDays operator--(int);
};
void NumDays::simplify()
{
hour = 8*day + hour;
day = hour / 8;
hour = hour % 8;
}
NumDays NumDays::operator++(int)
{
NumDays obj1;
hour++;
simplify();
return obj1;
}
NumDays NumDays::operator--(int)
{
NumDays obj1;
hour--;
simplify();
return obj1;
}
void setFirst(NumDays &);
void setSecond(NumDays &);
void addData(NumDays &, NumDays &, NumDays &);
int main()
{
NumDays first, second, third;
setFirst(first);
setSecond(second);
addData(first, second, third);
}
void setFirst(NumDays &obj1)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours." << endl;
cin >> day >> hour;
obj1.setData(day, hour);
}
void setSecond(NumDays &obj2)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours again." << endl;
cin >> day >> hour;
obj2.setData(day, hour);
}
void addData(NumDays &obj1, NumDays &obj2, NumDays &obj3)
{
for (int k = 0; k < 8; k++)
{
obj3 = obj1++;
cout << " First Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " First Data: " << obj1.getDay() << " day(s), "
<< obj1.getHour() << " hour(s).\n";
}
cout << endl;
for (int l = 0; l < 8; l++)
{
obj3 = obj2++;
cout << " Second Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " Second Data: " << obj2.getDay() << " day(s), "
<< obj2.getHour() << " hour(s).\n";
}
}
当我运行文件时,obj3有0天零0个小时,而obj2增加.怎么会这样呢? 当我尝试将其作为obj3 = obj2且没有任何后缀符号时,它将其复制过来.因此,我假设从obj2获取数据没有问题.但是,当我包含postfix运算符时,数据将变为0和0.
When I run the file, the obj3 have 0 days and 0 hours and the obj2 increases. How come it is like this? When I tried it as obj3 = obj2 without any postfix signs, it copies it over. So that I assume that there is no problem on getting data from obj2. But when I include the postfix operator, the data becomes 0 and 0.
推荐答案
您几乎完全正确.首先,我对您的班级做了一些更改.
You've got it almost right. First, I made a few changes to your class.
class NumDays
{
private:
int hour;
int day;
void simplify();
public:
NumDays(int dy = 0, int hr = 0) : hour(hr), day(dy) { simplify(); }
void setData(int d, int h)
{
hour = h;
day = d;
simplify();
}
int getHour() const { return hour; }
int getDay() const { return day; }
friend ostream& operator<<(ostream& out, const NumDays &nd) {
return out << " First Data: " << nd.getDay() << " day(s), "
<< nd.getHour() << " hour(s).";
}
NumDays operator++(int);
};
我做的第一件事是重写构造函数以使其具有默认参数并使用现代样式.
The first thing I did was to rewrite the constructor to have default arguments and to use a modern style.
我要做的第二件事是将const
添加到不(也不应该)修改对象的各种成员函数中.
The second thing I did was to add const
to various member functions that don't (and shouldn't) modify the object.
我做的第三件事是添加一个ostream
提取器作为friend
函数.纯粹是为了方便对问题进行故障诊断.
The third thing I did was to add an ostream
extractor as a friend
function. This was purely for convenience to troubleshoot the problem.
这是新版本的postincrement运算符的样子:
Here's what the new version of the postincrement operator looks like:
NumDays NumDays::operator++(int)
{
NumDays obj1(*this);
hour++;
simplify();
return obj1;
}
唯一的区别是,obj1
的创建现在使用默认的编译器生成的副本构造函数.这很重要,因为我们需要为后增量运算符返回未增量值的副本. (您的后缀递减运算符也需要类似的修正.)
The only difference is that the creation of obj1
now uses the default compiler-generated copy constructor. This is important because we need to return a copy of the unincremented value for a postincrement operator. (Your postfix decrement operator needs a similar fix.)
我没有对您的simplify()
例程进行任何更改.
I made no changes to your simplify()
routine.
然后我用以下代码对其进行了测试:
I then tested it with this code:
int main()
{
NumDays nd(2,3);
cout << nd++ << endl; // prints 2, 3
cout << nd << endl; // prints 2, 4
return 0;
}
现在一切正常.
这篇关于重载postfix运算符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!