我专门为该类重载了增量运算符,但使用后缀增量,编译器只显示第一个c1.getCount()
,然后输出:“(进程 17100)退出,代码为 -1073741571。”
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
class Counter
{
private:
int count;
public:
Counter() : count(0)
{}
Counter(int c) : count(c)
{}
int getCount()
{
return count;
}
Counter operator++ ()
{
return Counter(++count);
}
Counter operator++ (int)
{
return operator++ (count++);
}
};
int main()
{
Counter c1;
Counter c2;
cout << c1.getCount() << endl;
++c1;
c2++;
cout << c2.getCount() << endl;
cout << c1.getCount() << endl;
return 0;
}`