9.4.5 运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
1、加号运算符重载
**作用:**实现两个自定义数据类型相加的运算。运算符重载,也可以发生函数重载
#include<iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
//1、 通过成员函数重载+号
//Person operator+(Person& p)
//{
// Person temp;
// temp.m_A = p.m_A + this->m_A;
// temp.m_B = p.m_B + this->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_A + 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_A + num;
return temp;
}
void test11()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载本质调用
//Person p3 = p1.operator+(p2);
//全局函数重载本质的调用
//Person p3 = operator+(p1, p2);
Person p3 = p1 + p2;//简化版
//运算符重载,也可以发生函数重载
Person p4 = p1 + 16;
cout << "p3.m_A = " << p3.m_A << endl;
cout << "p3.m_B = " << p3.m_B << endl;
cout << "p4.m_A = " << p4.m_A << endl;
cout << "p4.m_B = " << p4.m_B << endl;
}
int main()
{
test11();
system("pause");
return 0;
}
运行结果:
**总结1:**对于内置的数据类型的表达式的运算符是不可能改变的
**总结2:**不要滥用运算符重载
2、 左移运算符重载
**作用:**可以输出自定义数据类型
#include<iostream>
using namespace std;
//左移运算符重载
class Person
{
friend ostream& operator<<(ostream& cout, Person& p); //本质operator<<(cout,p) 简化为 cout<<p;
public:
Person(int a,int b)
{
m_A = a;
m_B = b;
}
private:
//利用成员函数重载,左移运算符,p operator<<(cout) 简化版本 p<<cout
//不能利用成员函数重载<<运算符,因为无法实现 cout 在左侧
//void operator<<(cout)
//{
//}
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream &operator<<(ostream &cout,Person &p) //本质operator<<(cout,p) 简化为 cout<<p;
{
cout << "m_A = " << p.m_A << "m_B = " << p.m_B;
return cout;
}
void test21()
{
Person p(10, 10);;
//p.m_A = 10;
//p.m_B = 10;
//cout << p;
cout << p << " hello "<<endl;
//cout << "p.m_A = " <<p.m_A<< endl;
}
int main()
{
test21();
system("pause");
return 0;
}
运行结果:
**总结:**重载左移运算符配合友元可以实现输出自定义数据类型
3、递增运算符重载
**作用:**通过重载递增运算符,实现自己的整形数据
#include<iostream>
using namespace std;
//重载递增运算符
class MyInerger
{
//友元函数声明
friend ostream& operator<<(ostream& cout, MyInerger myint);
public:
MyInerger()
{
m_Num = 0;
}
//重载前置++运算符
//MyInerger operator++()
MyInerger & operator++()
{
//先进行++运算
m_Num++;
//将自身 主体返回
return *this;
}
//重载后置++运算符
//后置递增要返回的是值,要和前置递增区分开
MyInerger operator++(int ) //int 是占位参数,可以用于区分前置++
{
//先 记录当时结果
MyInerger temp = *this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
void test31()
{
MyInerger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test32()
{
MyInerger myint;
cout << myint++ << endl;
cout << myint << endl;
}
//重载<<运算符
ostream& operator<<(ostream& cout, MyInerger myint)
{
cout << myint.m_Num;
return cout;
}
int main()
{
//test31();
test32();
system("pause");
return 0;
}
运行结果:
**总结:**前置递增返回的是引用,后置递增返回的是值。
练习:递减运算符
#include<iostream>
using namespace std;
//递减运算符重载
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myInt);
public:
//构造函数初始化
MyInteger()//前置--
{
m_Num = 0;
}
MyInteger& operator--()
{
m_Num--;
return *this;
}
//后置--
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 test031()
{
MyInteger myint;
//cout << --myint << endl;
cout << myint-- << endl;
cout << myint << endl;
}
int main()
{
test031();
system("pause");
return 0;
}
运行结果:
4、赋值运算符重载
C++中编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符 operator=,对属性进行拷贝
如果类中有属性执行堆区,做赋值操作时也会出现深浅拷贝问题
示例代码:
#include<iostream>
using namespace std;
//赋值运算符重载
class Person
{
public:
Person(int age)
{
m_age = new int(age); //在堆区开辟一块内存存放age
}
~Person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
//重载 赋值运算符
//Person operator=(Person& p)
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 test41()
{
Person p1(18);
Person p2(28);
Person p3(38);
p3=p2 = p1;//赋值操作
cout << "p1.m_age = " << *p1.m_age << endl;
cout << "p2.m_age = " << *p2.m_age << endl;
cout << "p3.m_age = " << *p3.m_age << endl;
}
int main()
{
test41();
//int a = 10;
//int b = 20;
//int c = 30;
//c = b = a;
//cout << "a = " << a << endl;
//cout << "b = " << b << endl;
//cout << "c = " << c << endl;
system("pause");
return 0;
}
运行结果:
中间过程中遇到的问题:
wntdll.pdb 包含查找模块 ntdll.dll 的源文件所需的调试信息
分析问题产生的原因:
程序崩溃的原因:系统提供的浅拷贝导致内存空间重复释放
解决问题方法:
进行深拷贝操作,为m_age变量重新开辟一块存放内存空间。
5、关系运算符重载
**作用:**重载关系运算符,可以让两个自定义类型对象进行对比操作。
示例代码:
#include<iostream>
using namespace std;
//关系运算符重载
class Person
{
public:
Person(string name,int age)
{
m_name = name;
m_age = age;
}
//重载 == 号
bool operator==(Person &p)
{
if (this->m_age == p.m_age && this->m_name==p.m_name)
{
return true;
}
return false;
}
//重载 != 号
bool operator!=(Person& p)
{
if (this->m_age == p.m_age && this->m_name == p.m_name)
{
return false;
}
return true;
}
string m_name;
int m_age;
};
void test51()
{
Person p1("张三", 18);
Person p2("李四", 19);
Person p3("张三", 18);
if (p1 == p3)
{
cout << "p1和p3是相等的!" << endl;
}
else
{
cout << "p1和p3是不相等的!" << endl;
}
if (p1 != p2)
{
cout << "p1和p2是不相等的!" << endl;
}
else
{
cout << "p1和p2是是相等的!" << endl;
}
}
int main()
{
test51();
system("pause");
return 0;
}
运行结果:
写博客记录技术,也是我一个优点
贷款发放时间
6、函数调用运算符重载
- 函数调用运算符()也可以发生重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
示例代码:
#include<iostream>
using namespace std;
#include<string>
//函数调用运算符重载
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
int operator()(int number1,int number2)
{
return number1 + number2;
}
};
void myPrint(string test)
{
cout << test << endl;
}
void test61()
{
MyPrint p1;
p1("hello world");//由于使用起来非常像函数的调用,所以称之为仿函数
myPrint("hello world");
}
void test62()
{
MyAdd myadd;
int result=myadd(100, 100);
cout << "result = " << result << endl;
//匿名函数对象:MyAdd()
cout << MyAdd()(10,10) << endl;
}
int main()
{
//test61();
test62();
system("pause");
return 0;
}
运行结果: