概念:对已有的运算符重新定义,赋予另一种功能,以适应不同的数据类型
1.加号运算符重载
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class Person {
public:
//1.成员函数重载+号
Person operator+(Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载调用的本质
//Person p3=p1.operator+(p2);
//全局函数重载调用的本质
//Person p3=operator+(p1+p2);
Person p3 = p1 + p2;
cout << "p3.m_A: " << p3.m_A << endl;
cout << "p3.m_B: " << p3.m_B << endl;
}
//全局函数重载+号
Person operator+(Person& p1, Person& p2) {
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
2.左移运算符重载
作用:输出自定义数据类型
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class Person {
public:
//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream& operator<<(ostream& cout, Person& p) {
cout << "m_A: " << p.m_A << endl;
cout << "m_B: " << p.m_B << endl;
return cout;
}
void test01() {
Person p;
p.m_A = 10;
p.m_B = 10;
cout << p<<endl; //如果上面不是ostream& operator开头的话 这行加endl会报错;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
3.递增运算符重载
重载递增运算符,实现自己的整形数据
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class MyInteger {
friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
MyInteger() {
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++() { //前置递增必须返回引用,为了一直对一个数据进行递增操作
//先进行++运算
m_Num++;
//再将自身返回
return *this;
}
//重载后置++运算符
//void operator++(int) int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int) { //后置递增必须返回值而不能是引用,temp是局部变量,返回值的话出区就被释放了
//先记录当时结果
MyInteger temp = *this;
//然后递增
m_Num++;
//最后将记录结果返回
return temp;
}
private:
int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream& cout, MyInteger myint) {
cout << myint.m_Num;
return cout;
}
void test01() {
MyInteger myint;
cout << ++myint << endl;
}
void test02() {
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
4.赋值运算符重载
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class Person {
public:
Person(int age) {
//将年龄数据开辟到堆区
m_Age = new int(age);
}
~Person() {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
//重载赋值运算符
Person& operator=(Person& p) {
//编译器提供的是浅拷贝
//m_Age=p.m_Age;
//应该判断是否有属性在堆区,如果有,先释放干净,然后再深拷贝
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
int *m_Age;
};
void test01() {
Person p1(18);
cout << "p1年龄:" << *p1.m_Age << endl;
Person p2(20);
p2 = p1;
cout << "p2年龄: " << *p2.m_Age << endl;
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
5.关系运算符重载
重载关系运算符可以让两个自定义类型对象进行对比操作
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class Person {
public:
Person(string name, int age) {
m_Name = name;
m_Age = age;
}
//重载==号
bool operator==(Person& p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
}
return false;
}
//重载!=号
bool operator!=(Person& p) {
if (this->m_Name != p.m_Name && this->m_Age != p.m_Age) {
return true;
}
return false;
}
string m_Name;
int m_Age;
};
void test01() {
Person p1("tom",18);
Person p2("tom",18);
if (p1 == p2) {
cout << "p1和p2是相等的" << endl;
}
else {
cout << "p1和p2是不相等的" << endl;
}
if (p1 != p2) {
cout << "p1和p2是不相等的" << endl;
}
else {
cout << "p1和p2是相等的" << endl;
}
}
int main(int argc,char**argv) {
test01();
system("pause");
return 0;
}
6.函数调用运算符重载
函数调用运算符()也可以重载
由于重载后使用的方式很像函数的调用,因此成为仿函数
仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>
#include<numeric>
#include<map>
#include<set>
#include<utility>
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
};
void test01() {
MyPrint myprint;
myprint("hello world");
}
//仿函数非常灵活,没有固定写法
//加法类
class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
};
void test02() {
MyAdd myadd;
int ret=myadd(10, 20);
cout << "ret= " << ret << endl;
//或者:匿名函数对象
cout << MyAdd()(10, 20) << endl;
}
int main(int argc,char**argv) {
test01();
test02();
system("pause");
return 0;
}