运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载
cpp
#include<iostream>
using namespace std;
class Person
{
public:
int m_A;
int m_B;
//本质是p3=p1.operator+(p2);
//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;
//}
};
//本质是p3=operator+(p1,p2);
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& p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.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;
cout << p3.m_A << endl;
cout << p3.m_B << endl;
}
int main()
{
test01();
}
总结:
- 运算符重载也可以发生函数重,例如p3=p1+10;
- 对于内置的数据类型的表达式的的运算符是不可能改变的
- 不要滥用运算符重载
左移运算符重载
例如我们想cout<<p;直接输出p这个对象就打印p的内部属性,就可以重载左移运算符
cpp
#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)
{
cout << p.m_A << endl << p.m_B << endl;
return cout;
}
void test01()
{
Person p;
cout << p << endl;
}
int main()
{
test01();
}
注意:
- 利用成员函数重载左移运算符p.operator<<(cout)简化版本p<<cout
- 所以不会利用成员函数重载<<运算符,因为无法实现cout在左侧
- 一般类的成员变量我们会设置为私有权限,为了保证运算符的正常使用,可以将重载后的函数作为友元。
递增运算符重载
作用:通过重载递增运算符,实现自己的整型数据
cpp
#include<iostream>
using namespace std;
class MyInteger {
friend ostream& operator<<(ostream& cout, MyInteger mi);
public:
MyInteger()
{
m_Num = 0;
}
MyInteger& operator++() {
m_Num++;
return *this;
}
//int做占位参数,与前置递增做区分
MyInteger operator++(int) {
MyInteger temp = *this;
m_Num++;
return temp;
}
private:
int m_Num = 0;
};
ostream& operator<<(ostream& cout, MyInteger mi)
{
cout << mi.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout << ++(++myint) << endl;;
}
void test02()
{
MyInteger myint;
cout << myint++ << endl;;
cout << myint << endl;
}
int main()
{
//test01();
test02();
}
总结:前置递增返回引用,后置递增返回值
赋值运算符重载
C++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题。堆区需要我们手动释放,那么拷贝之后,会在析构函数里释放两次同一片地址空间,就会报错。
cpp
#include<iostream>
using namespace std;
class Person {
public:
Person(int age) {
this->age = new int(age);
}
int* age;
~Person()
{
delete age;
age = NULL;
}
Person& operator=(Person& p)
{
if (age != NULL)
{
delete age;
age = NULL;
}
this->age = new int(*p.age);
return *this;
}
};
void test01()
{
Person p1(18);
cout << *p1.age << endl;
Person p2(20);
cout << *p2.age << endl;
Person p3(30);
p3 = p2 = p1;
cout << *p1.age << endl;
cout << *p2.age << endl;
cout << *p3.age << endl;
}
int main()
{
test01();
}
关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
cpp
#include<iostream>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->name = name;
this->age = age;
}
bool operator==(Person& p)
{
if (name == p.name && age == p.age)
{
return true;
}
return false;
}
string name;
int age;
};
void test()
{
Person p1("Alice", 30);
Person p2("Alice", 31);
if (p1 == p2)
{
cout << "p1和p2相等" << endl;
}
else {
cout << "p1和p2不相等" << endl;
}
}
int main()
{
test();
}
函数调用运算符重载
- 函数调用运算符()也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
cpp
#include<iostream>
using namespace std;
#include<string>
class MyPrint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
class MyAdd {
public:
int operator()(int a, int b)
{
return a + b;
}
};
void Pr(string test)
{
cout << test << endl;
}
void test1()
{
MyPrint mp;
mp("hello,world");//由于使用起来非常类似与函数调用,所以叫仿函数
Pr("hello,world");
}
void test2()
{
MyAdd ma;
int result = ma(10, 20);
cout << result << endl;
//匿名函数对象
cout << MyAdd()(100, 200) << endl;
}
//仿函数非常灵活,没有固定写法.
int main()
{
test2();
}