对已有的运算符重新定义,赋予其另一个种功能,以适应不同的数据类型
目录
1.加号运算符重载
#include<iostream>
using namespace std;
class Person {
public:
Person() {
}
Person(int a, int b) {
this->mA = a;
this->mB = b;
}
//重载+号运算符
Person operator+(const Person& p) {
Person tmp;
tmp.mA = this->mA + p.mA;
tmp.mB = this->mB + p.mB;
return tmp;
}
void show() {
cout << this->mA << " " << this->mB << endl;
}
friend Person operator+(const Person& p, int val);
private:
int mA, mB;
};
//运算符重载可以发生函数重载也可以用全局函数
Person operator+(const Person& p, int val) {
Person tmp;
tmp.mA = p.mA + val;
tmp.mB = p.mB + val;
return tmp;
}
int main()
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p1 + p2;
p3.show();
p3 = p1 + 50;
p3.show();
return 0;
}
2.左移运算符重载
可以输出自定义的数据类型
#include<iostream>
using namespace std;
class Person {
public:
Person() {
}
Person(int a, int b) {
this->mA = a;
this->mB = b;
}
//重载+号运算符
Person operator+(const Person& p) {
Person tmp;
tmp.mA = this->mA + p.mA;
tmp.mB = this->mB + p.mB;
return tmp;
}
//成员函数实现不了 p<<cout不是我们想要的效果
//void operator<<(Person& p) {
//}
void show() {
cout << this->mA << " " << this->mB << endl;
}
friend Person operator+(const Person& p, int val);
friend ostream& operator<<(ostream& out, Person& p);
private:
int mA, mB;
};
//运算符重载可以发生函数重载
Person operator+(const Person& p, int val) {
Person tmp;
tmp.mA = p.mA + val;
tmp.mB = p.mB + val;
return tmp;
}
//全局函数实现左移重载 用ostream&是为了链式编程
ostream& operator<<(ostream &out, Person& p)
{
out << p.mA << " " << p.mB ;
return out;
}
int main()
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p1 + p2;
cout << p3 <<" " << "hehe" << endl;;
p3 = p1 + 50;
p3.show();
return 0;
}
3.++运算符
#include<iostream>
using namespace std;
class MyInterger {
friend ostream& operator<<(ostream& out, MyInterger m);
public:
MyInterger() {
m_num = 0;
}
//前置++
MyInterger& operator++() {
m_num++;
return *this;
}
//后置++
MyInterger operator++(int) {
MyInterger temp = *this;
m_num++;
return temp;
}
private:
int m_num;
};
ostream& operator<<(ostream& out, MyInterger m) {
out << m.m_num;
return out;
}
int main()
{
MyInterger m;
m++;
cout << m << endl;
++m;
cout << m << endl;
return 0;
}
4.赋值运算符=
避免浅拷贝问题,系统提供默认的赋值运算符=是浅拷贝
#include<iostream>
using namespace std;
class Person {
public:
Person(int age=0) {
mage = new int(age);
}
~Person() {
if (mage != nullptr) {
delete mage;
mage = nullptr;
}
}
Person& operator=(Person& p) {
if (mage != nullptr) {
delete mage;
mage = nullptr;
}
mage = new int(*p.mage);
return *this;
}
int* mage;
};
int main()
{
Person p1(20);
Person p2 ;
p2 = p1;
cout << *p2.mage << endl;
return 0;
}
5.关系运算符重载 ==和!=
判断对比操作
#include<iostream>
using namespace std;
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->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 == p)
return false;
return true;
}
string m_name;
int m_age;
};
int main()
{
Person p1("hhh", 20);
Person p2("hhh", 10);
if (p1 == p2)
cout << "p1==p2" << endl;
if (p1 != p2)
cout << "p1!=p2" << endl;
return 0;
}
6.函数调用运算符重载
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;
class Myprint {
public:
void operator()(string text) {
cout << text << endl;
}
};
int main()
{
Myprint a;
a("hello");
return 0;
}