运算符重载
概念: 对于已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
1、加号运算符重载
//加号运算符重载
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;
};
//2、全局函数重载+号
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;
}
//+号函数重载
Person operator+(Person& p, int num) {
Person temp;
temp.m_a = p.m_a + num;
temp.m_b = p.m_b + num;
return temp;
}
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 + p2;
//成员函数重载本质调用:Person p3 = p1.operator+(p2);
//全局函数重载本质调用:Person p3 = operator+(p1,p2);
cout << "p3.m_a = " << p3.m_a << endl;
cout << "p3.m_b = " << p3.m_b << endl;
//运算符重载 也可以发生函数重载
Person p4 = p1 + 50;
cout << "p4.m_a = " << p4.m_a << endl;
cout << "p4.m_b = " << p4.m_b << endl;
}
int main() {
test01();
return 0;
}
总结:
1、对于内置的数据类型的表达式的运算符是不可能改变的
2、不要滥用运算符重载
2、左移运算符重载:
作用: 可以输出自定义数据类型
//左移运算符重载
class Person {
public:
//不利用成员函数重载运算符,因为无法实现cout在左侧
//void operator<<(Person &p)
int m_a;
int m_b;
};
//只能利用全局函数重载左移运算符
ostream &operator <<(ostream& cout, Person& p) {
cout << "m_a = " << p.m_a << "m_b = " << p.m_b;
return cout;
}
void test01() {
Person p;
p.m_a = 10;
p.m_b = 10;
cout << p << endl;
}
3、递增运算符重载:
//重载递增运算符
class MyInteger {
friend ostream& operator <<(ostream& cout, MyInteger myint);
public:
MyInteger() {
m_num = 0;
}
//重载前置++运算符,返回引用是为了一直对一个数据进行递增操作
MyInteger &operator ++ () {
m_num++;
return *this;
}
//重载后置++运算符,int代表占位参数,可以用于区分前置和后置递增,只能用Int
MyInteger operator ++(int) {
MyInteger temp = *this;
m_num++;
return temp;
}
private:
int m_num;
};
//重载左移运算符
ostream& operator <<(ostream& cout, MyInteger myint) {
cout << myint.m_num;
return cout;
}
void test02() {
MyInteger myint2;
cout << myint2++ << endl;
cout << myint2 << endl;
}
void test01() {
MyInteger myint;
cout << ++myint << endl;
}
4、赋值运算符重载:
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);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_age << endl;
cout << "p2的年龄为:" << *p2.m_age << endl;
cout << "p3的年龄为:" << *p3.m_age << endl;
}
5、关系运算符重载
作用: 重载关系运算符,可以让两个自定义类型对象进行对比操作
class Person {
public:
Person(string name, int age) {
m_name = name;
m_age = age;
}
string m_name;
int m_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 false;
}
return true;
}
};
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;
}
}
6、函数调用运算符重载
函数调用运算符 ( ) 也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
//函数调用运算符重载
//打印输出类
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 addre = myadd(100, 100);
cout << "addre = " << addre << endl; //输出 addre = 200
//匿名函数对象,看到类名()想到匿名函数对象,后面的(参数)就是在调用重载函数
cout << Myadd()(100, 100) << endl; //输出 200
}