完整代码如下:
#include <iostream>
using namespace std;
class Person3 {
friend ostream& operator<<(ostream& cout, Person3& p1);
public:
Person3(int a, int b) :m_A(a), m_B(b) {
}
Person3& operator++() {
this->m_A += 10;
this->m_B += 10;
return *this;
}
//返回值
const Person3 operator++(int) {
Person3 temp = *this;
this->m_A += 10;
this->m_B += 10;
return temp;
}
private:
int m_A;
int m_B;
};
//运算符重载函数的重载
ostream& operator<<(ostream& cout, Person3& p1) {
cout << "m_A = " << p1.m_A << " m_b= " << p1.m_B;
return cout;
}
void test02() {
Person3 p1(10, 10);
Person3 p2(50, 50);
cout << p1 << endl;
cout << ++(++p1) << endl;
p2++;
cout << p2 << endl;
cout << p2++ << endl;
}
int main() {
test02();
return 0;
}
operator<<
第二个参数改为const Person3&
,因为后缀++返回的类型是const Person3
,不能传递给Person3&
类型