一、成员函数实现运算符功能(不重载)
class Person {
public:
int age;
Person() : age(18) {}
Person add(const Person& p) {
Person temp;
temp.age = this->age + p.age;
return temp;
}
};
调用方式:Person p3 = p1.add(p2);
add函数中的参数加个&
的优点(不考):
- 节约程序运行时间。
- 节约内存,避免调用拷贝构造函数。如果去掉
&
,就会调用拷贝构造函数,多占一份内存。
前面加个const
是为了避免被修改。
二、成员函数重载运算符
1、重载"+"
class Person {
public:
int age;
Person() : age(18) {}
Person operator+(const Person& p) {
Person temp;
temp.age = age + p.age;
return temp;
}
};
函数名是关键字 operator
和其后要重载的运算符符号构成。
调用方式:Person p3 = p1 + p2;
(等价于p1.operator+(p2)
)
2、重载流插入运算符"<<"
class Person {
public:
int id;
int age;
Person() : id(0), age(18) {}
void operator<<(ostream& cout) {
cout << id << "," << age;
}
};
调用方式:p << cout;
(等价于p.operator<<(cout);
)
注意这里cout
只能在右边,因为这里是在person
类里面的成员函数,所以p
必须在左边。如果要让p
在右边,那么只能使用下面的全局函数。
3、重载关系运算符
class Person {
public:
int id;
int age;
Person(int _id, int _age) : id(_id), age(_age) {}
//带参数的构造函数
bool operator==(const Person& p) {
return (id==p.id && age==p.age);
}
bool operator!=(const Person& p) {
return (id != p.id || age != p.age);
}
};
调用方式:p1 == p2;
,p1 != p2;
4、重载自增运算符"++"(前缀)
class Person {
public:
int age;
Person() : age(18) {}
Person& operator++() {
++age;
return *this;
}
};
ostream& operator<<(ostream& cout, const Person& p) {
cout << p.age;
return cout;
}
调用方式:cout << ++(++p) << endl;
注意这里如果把person&
中的&
去掉,则++p
会创建一个新的person对象,这个新的对象与原来的对象p没有任何关系,所以当进行cout << ++(++p) << endl;
之后再进行cout << p << endl;
,得到的是两个不同的值(p
只自加了一次)。
5、重载自增运算符"++"(后缀)
注意不支持(a++)++
。
class Person {
public:
int age;
Person() : age(18) {}
Person operator++(int) {
Person temp = *this;
++age;
return temp;
}
};
ostream& operator<<(ostream& cout, const Person& p) {
cout << p.age;
return cout;
}
用int
来区分++a
和a++
。
调用方式:cout << p++ << endl;
6、重载"="
class Person {
public:
int* age;
Person(int _age) : age(new int(_age)) {}
~Person() {
delete age;
}
注意new
要和delete
配对,否则有可能出现内存溢出。(重点)
如果创建两个对象p1,p2
,Person p1(18); Person p2(28);
然后使p2=p1;
(调用的是拷贝构造函数,浅拷贝),则会报错,因为p2
指向了p1
的18,导致p1
析构了两次,从而出错,下面来看深拷贝。
class Person {
public:
int* age;
Person(int _age) : age(new int(_age)) {}
~Person() {
delete age;
}
Person& operator=(Person& p) {
age = new int(*p.age);
return *this;
}
};
这时如果p2=p1;
则调用的是"="重载函数,而Person p3 = p1;
调用的依然是拷贝构造函数。
但这个时候如果有p2=p1;
,其实存在内存泄露,因为这个时候p2
创建了一个新的18,而原来的28没有被删除。所以要把重载函数改成下面这种形式。
Person& operator=(Person& p) {
if (age != NULL) {
delete age;
}
age = new int(*p.age);
return *this;
}
这个时候就把原来的28delete
了然后创建了一个新的18。(重点)
这里也可以实现链式调用p3=p2=p1
(不考)。
7、仿函数(重载函数调用运算符 “()”)
class Adder {
public:
int operator()(int a, int b) {
return a + b;
}
};
在主函数中调用如下:
void main() {
Adder adder;
cout << adder(2, 3) << endl;
}
外观上看起来就是一个函数声明+函数调用。(可能考可能不考)
三、全局函数重载运算符
1、重载"+"
class Person {
public:
int age;
Person() : age(18) {}
};
Person operator+(const Person& p1, const Person& p2) {
Person temp;
temp.age = p1.age + p2.age;
return temp;
}
调用方式:Person p3 = p1 + p2;
(等价于operator+(p1,p2)
)
2、重载流插入运算符"<<"
class Person {
public:
int id;
int age;
Person() : id(0), age(18) {}
};
void operator<<(ostream& cout, const Person& p) {
cout << p.id << "," << p.age;
}
调用方式:cout << p;
等价于operator<<(cout, p);
。
若将参数调换位置依然是p << cout;
但是这种方式不能实现cout << p1 << p2;
,下面来看另一种链式编程的实现方式。(必考)
class Person {
public:
int id;
int age;
Person() : id(0), age(18) {}
};
ostream& operator<<(ostream& cout, const Person& p) {
cout << p.id << "," << p.age;
return cout;
}
调用方式:cout << p1 << p2;
(等价于operator<<(operator<<(cout, p), p1);
)