对于内置的数据类型,编译器知道如何进行运算,但是对于自定义数据类型,编译器不知道如何运算,利用运算符重载 可以让符号有新的含义。
定义重载的运算符就像定义函数,只是函数的名字为
operator@
,其中
@
代表被重载的运算符。
运算符是一元还是二元取决于运算符函数中参数的个数。
- 全局函数(一个参数是一元,两个参数是二元)
- 成员函数(一元没有参数、二元一个参数,此时该类的对象用作左值)
不要滥用运算符重载,除非有需求,不能对内置数据类型进行重载 。
加号运算符重载
利用加号重载 ,实现Person数据类型相加操作 p1 + p2
class Person
{
public:
Person() {};
//使用属性列表进行赋值
Person(int a, int b) :m_A(a), m_B(b)
{};
//利用成员函数实现加号运算符重载
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;
};
//利用全局函数实现加号运算符重载
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 test()
{
Person p1(10, 10);
Person p2(20, 20);
//Person p3 = p1 + p2;
//Person p3 = operator+(p1, p2); //全局函数本质
Person p3 = p1.operator+(p2); //成员函数本质
cout << "p3.m_A = " << p3.m_A << " p3.m_B = " << p3.m_B << endl;
//运算符重载 可不可以发生 函数重载? 可以
p1 + 10;
}
- 利用成员函数 和 全局函数 都可以实现重载
- 关键字
operator +
- 成员函数本质
p1.operator+(p2)
- 全局函数本质
operator+(p1,p2)
- 简化为
p1 + p2
(使用简化版时成员函数、全局函数只能使用一个) - 运算符重载 也可以发生函数重载
左移运算符重载
class Person
{
friend ostream& operator<<(ostream& cout, Person& p1);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
int m_A;
int m_B;
};
//利用全局函数 实现左移运算符重载
ostream& operator<<(ostream& out, Person& p1)
{
cout << "m_A = " << p1.m_A << " m_B = " << p1.m_B;
return cout;
}
void test()
{
Person p1(10, 10);
cout << p1 << endl;
}
- 对于自定义数据类型,不可以直接用 cout << 输出,需要重载 左移运算符
- 如果利用成员 函数重载 ,无法实现让cout 在左侧,因此不用成员重载
//试图利用成员函数 做<<重载
void operator<<( Person & p)
{
}
//调用,只能简化为p<<cout,不满足需求
p.operator<<(cout)
- 利用全局函数,实现左移运算符重载
ostream& operator<<(ostream &cout, Person & p1)
//利用全局函数 实现左移运算符重载
//形参out是实参cout的别名,所以函数体中也可以直接使用实参名
ostream& operator<<(ostream& out, Person& p1)
{
cout << "m_A = " << p1.m_A << " m_B = " << p1.m_B;
//返回原始对象,方便链式编程
return cout;
}
- 如果想访问类中私有内存,可以将全局函数配置友元实现
递增运算符重载
- 前置递增
MyInter& operator++()
- 后置递增
MyInter operator++(int)
- 前置++ 效率高于 后置++ 效率 ,因为后置++会调用拷贝构造,创建新的数据(一个临时对象)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyInter
{
friend ostream& operator<<(ostream& cout, MyInter& myInt);
public:
MyInter()
{
m_Num = 0;
}
//前置++ 重载,返回计算后的对象本身
MyInter& operator++()
{
this->m_Num++;
return *this;
}
//后置++ 重载,返回初始状态,再++
MyInter operator++(int)
{
//先记录初始状态
MyInter temp = *this;
this->m_Num++;
return temp;
}
private:
int m_Num;
};
//自定义数据类型重载<<,允许输出
ostream& operator<<(ostream& cout, MyInter& myInt)
{
cout << myInt.m_Num;
return cout;
}
void test01()
{
MyInter myInt;
cout << ++(++myInt) << endl;
cout << myInt << endl;
}
void test02()
{
MyInter myInt;
MyInter ret = myInt++;
cout << ret << endl;
cout << myInt << endl;
}
int main() {
test01();
test02();
//int a = 0;
//cout << ++(++a) << endl;
//cout << a << endl;
//int b = 0;
//cout << (b++)++ << endl; //后置++返回的是值,不允许再进行后置++操作
system("pause");
return EXIT_SUCCESS;
}
指针运算符重载
- 智能指针,用途: 托管
new
出来的对象的释放 - 设计smartPoint智能指针类,内部维护
Person *
,在析构时候释放堆区new出来的person对象 - 重载
-> *
让 sp智能指针用起来向真正的指针
class Person
{
public:
Person(int age)
{
cout << "Person的有参构造调用" << endl;
this->m_Age = age;
}
void showAge()
{
cout << "年龄为: " << this->m_Age << endl;
}
~Person()
{
cout << "Person的析构调用" << endl;
}
int m_Age;
};
class SmartPoint
{
public:
SmartPoint(Person* person)
{
this->m_person = person;
}
//重载->运算符
Person* operator->()
{
return this->m_person;
}
//重载 * 运算符,返回的是Person的引用,因为返回原始对象就可以,不需要通过赋值创建新的对象
Person& operator*()
{
return *m_person;
}
~SmartPoint()
{
if (this->m_person)
{
delete this->m_person;
this->m_person = NULL;
}
}
private:
//维护一个Person类型的指针
Person* m_person;
};
void test()
{
//Person * p = new Person(18);
//delete p;
//利用智能指针 管理 new出来的person的释放操作,方法中的对象都创建在栈中,方法执行完后会调用对应的析构函数进行释放,起到自动管理指针的作用
SmartPoint sp(new Person(18));
//p->showAge();
//sp->返回的是(Person * )->showAge();
sp->showAge(); // 本质sp->->showAge(); 编译器简化为 sp->showAge();
//(*p).showAge();
//(*sp)解引用后返回的应该为Person类型的对象,对象再调用成员方法
(*sp).showAge();
}
总结
- 使用
new
创建的对象,创建在堆区,用完需要手动释放,否则只会在程序关闭后自动释放 - 自定义智能指针时,可以类比真实的指针,对比两者间所支持的操作
虽然可以创建SmartPerson进行管理指针,但是每创建一个新类型都需要创建对应的智能指针太繁琐,还是建议自己维护指针(new / delete)
赋值运算符重载
编译器会默认个一个类添加4个函数:默认构造(空实现)、析构(空实现) 、 拷贝构造(值拷贝) 、 operator=(值拷贝);如果类中有属性创建在堆区,利用编译器提供的 =
赋值运算就会出现堆区内存重复释放的问题
解决方案:利用深拷贝重载 =运算符
,函数声明如下Person& operator=( const Person &p)
,并自定义拷贝构造。
//编译器 默认给一个类4个函数 默认构造 析构 拷贝构造 (值拷贝) operator= (值拷贝)
class Person
{
public:
Person(char * name, int age)
{
this->m_Name = new char[strlen(name) + 1];
strcpy(this->m_Name, name);
this->m_Age = age;
}
//重载 =,使用深拷贝
Person& operator=( const Person &p)
{
//先判断原来堆区释放有内容,如果有先释放
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
//返回当前对象的引用,用来链式操作
return *this;
}
//拷贝构造,使用深拷贝
Person(const Person & p)
{
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
}
//析构,释放堆区创建的内容
~Person()
{
if (this->m_Name!=NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
}
char * m_Name;
int m_Age;
};
void test()
{
Person p1("Tom",10);
Person p2("Jerry",19);
p2 = p1;
Person p3("", 0);
p3 = p2 = p1;
//使用拷贝构造进行复制
Person p4 = p3;
cout << "p1姓名: "<< p1.m_Name << " p1年龄: " << p1.m_Age << endl;
cout << "p2姓名: "<< p2.m_Name << " p2年龄: " << p2.m_Age << endl;
cout << "p3姓名: " << p3.m_Name << " p3年龄: " << p3.m_Age << endl;
}
int main(){
test();
//类比原始类型的操作,进行自定义=的重载操作
/*int a = 10;
int b = 20;
int c;
c = a = b;
cout << "a = " << a << " b = " << b << " c = " << c << endl;*/
system("pause");
return EXIT_SUCCESS;
}
[]运算符重载
实现自定义数组类型[]取值及作为左值操作
- 函数声明
int& operator[](int index);
- 实现访问数组时候利用[] 访问元素,函数实现如下
//返回& 引用类型是为了作为左值,允许对其进行赋值操作如:a[100] = 10000;因为只有引用类型可以作为左值,普通原始类型不允许作为左值
int& MyArray::operator[](int index)
{
return this->pAddress[index];
}
函数调用运算符()重载
- 使用时候很像函数调用,因此称为仿函数,如下声明
void operator()(string text)
int operator()(int a,int b)
- 仿函数写法不固定,比较灵活
- 匿名函数对象的使用
cout << MyAdd()(1, 1) << endl;
// 匿名函数对象 特点:当前行执行完立即释放
#include <string>
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void MyPrint2(string str)
{
cout << str << endl;
}
void test01()
{
MyPrint myPrint;
myPrint("hello world"); // 仿函数本质是一个对象,使用时像函数,所以又可以称为函数对象
MyPrint2("hello world"); //普通函数调用
}
class MyAdd
{
public:
int operator()(int a,int b)
{
return a + b;
}
};
void test02()
{
MyAdd myAdd;
cout << myAdd(1, 1) << endl;
cout << MyAdd()(1, 1) << endl; // 匿名函数对象,特点:当前行执行完立即释放
}
总结
- 不要重载
&&
和||
,原因是无法实现短路特性 - 建议:将
<<
和>>
写成全局函数,其他可重载的符号写到成员即可。