概念:对已有运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型(运算符重载也可以实现函数重载)
- 加号运算符重载(实现两个自定义数据类型相加,operator+)
#include <iostream>
using namespace std;
class Person
{
public:
int m_A=10;
int m_B=10;
//通过成员函数重载
//Person operator+(Person& p)
//{
// Person tmp;
// tmp.m_A = this->m_A + p.m_A;
// tmp.m_B = this->m_B + p.m_B;
// return tmp;
//}
};
//全局函数重载
Person operator+(Person& p1,Person& p2)
{
Person tmp;
tmp.m_A = p1.m_A + p2.m_A;
tmp.m_B = p1.m_B + p2.m_B;
return tmp;
}
int main()
{
Person p1;
Person p2;
Person p3 = p1 + p2; //p1,operator+(p2)与operator+(p1,p2)简化
cout << p3.m_A << " " << p3.m_B << endl;
system("pause");
return 0;
}
- 左移运算符重载(<<)
- 作用:可以输出自定义数据类型
- 通常不用成员函数重载<<运算符,因为无法实现 cout在左侧
#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& cout, Person& p);
private:
int m_A=10;
int m_B=10;
};
//链式编程思想,可无限追加
ostream& operator<<(ostream& cout, Person& p) //本质 operator<<(cout,p),简化cout<<p
{
cout << "m_A=" << p.m_A << " m_B=" << p.m_B << endl;
return cout;
}
int main()
{
Person p;
cout << p <<endl;
system("pause");
return 0;
}
- 递增运算符重载
- 作用:通过递增运算符,实现自己的整型数据
#include <iostream>
using namespace std;
class myInt
{
friend ostream& operator<<(ostream& cout, myInt myint);
public:
myInt()
{
num = 0;
}
//重载前置++
myInt& operator++()
{
num++;
return *this;
}
//重载后置++
myInt operator++(int) //int代表占位参数,可以用于区分前后置++
{
myInt tmp = *this;
num++;
return tmp;
}
private:
int num;
};
ostream& operator<<(ostream& cout, myInt myint)
{
cout << "num= " << myint.num;
return cout;
}
int main()
{
myInt myint;
cout << myint++ << endl;
cout << myint++ << endl;
cout << ++myint << endl;
cout << ++myint << endl;
system("pause");
return 0;
}
- 赋值运算符重载
- c++给一个类添加4个函数(默认、析构、拷贝、赋值operator=(深浅拷贝问题))
#include <iostream>
using namespace std;
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)
{
//先判断是否有属性在堆区,如果有先释放,然后再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
int* m_Age;
};
int main()
{
Person p1(18);
Person p2(20);
Person p3(30);
cout << *p1.m_Age << endl;
cout << *p2.m_Age << endl;
cout << *p3.m_Age << endl;
p3 = p2 = p1;
cout << *p1.m_Age << endl;
cout << *p2.m_Age << endl;
cout << *p3.m_Age << endl;
system("pause");
return 0;
}
- 关系运算符重载
- 作用:让两个自定义数据类型比较
#include <iostream>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
bool operator==(Person& p)
{
if (m_Name == p.m_Name && m_Age == p.m_Age)
{
return true;
}
return false;
}
bool operator!=(Person& p)
{
if (m_Name != p.m_Name || m_Age != p.m_Age)
{
return true;
}
return false;
}
string m_Name;
int m_Age;
};
int main()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
if (p1 != p2)
{
cout << "不相等" << endl;
}
else
{
cout << "相等" << endl;
}
system("pause");
return 0;
}
- 函数调用运算符重载
- 函数调用运算符也可以重载
- 由于重载后使用方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
#include <iostream>
using namespace std;
class Print
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
int main()
{
Print myPrint;
myPrint("hello");
system("pause");
return 0;
}