文章内容转载自黑马程序员C++核心编程讲义,如有侵权,请联系作者删除


4.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加的运算

class Person {
public:
Person() {};
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//成员函数实现 + 号运算符重载
Person operator+(const Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}


public:
int m_A;
int m_B;
};

//全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
// Person temp(0, 0);
// temp.m_A = p1.m_A + p2.m_A;
// temp.m_B = p1.m_B + p2.m_B;
// return temp;
//}

//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int val)
{
Person temp;
temp.m_A = p2.m_A + val;
temp.m_B = p2.m_B + val;
return temp;
}

void test() {

Person p1(10, 10);
Person p2(20, 20);

//成员函数方式
Person p3 = p2 + p1; //相当于 p2.operaor+(p1)
cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;


Person p4 = p3 + 10; //相当于 operator+(p3,10)
cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

}

int main() {

test();

system("pause");

return 0;
}

自己写的:

#include<iostream>
using namespace std;
//加号运算符重载
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) {//注意这个函数需要放在test01前面,否则会报错
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.operator+(p2);

//全局函数重载本质:
Person p3 = operator+(p1, p2);

//两者都可以简化为以下版本
//Person p3 = p1 + p2;

//运算符重载 也可以发生函数重载
Person p4 = p1 + 100;//Person +int;

cout << p3.m_a << endl;
cout << p3.m_b << endl;

cout << p4.m_a << endl;
cout << p4.m_b << endl;
}
//1、成员函数重载+号
//2、全局函数重载+号
int main() {
test01();
return 0;
}

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

总结2:不要滥用运算符重载

4.5.2 左移运算符重载

作用:可以输出自定义数据类型

class Person {
friend ostream& operator<<(ostream& out, Person& p);

public:

Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}

//成员函数 实现不了 p << cout 不是我们想要的效果
//void operator<<(Person& p){
//}

private:
int m_A;
int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {
out << "a:" << p.m_A << " b:" << p.m_B;
return out;
}

void test() {

Person p1(10, 20);

cout << p1 << "hello world" << endl; //链式编程
}

int main() {

test();

system("pause");

return 0;
}

自己写的:

#include<iostream>
using namespace std;
class Person {
friend ostream& operator<<(ostream& out, Person& p);
friend ostream& operator<<(ostream& out, Person& p);

public:
Person(int a, int b) {
m_a = a;
m_b = b;
}
//public:
//利用成员函数重载 左移运算符

// 错误写法
//void operator<<(Person &p) { //等价于p.operator<<(p) 实际上需要传进两个对象,但我们只需要一个对象

//}

//另外的一种尝试
/*void operator<<(cout) {//等价于p.operator<<(cout) 简化版本 p << cout,输出结果为cout在右侧,与预期效果相反

}*/

//总结:不会利用成员函数重载<<运算符,因为无法实现 cout在左侧
//int m_a;
//int m_b;

//一般来说,数据成员设置权限为私有,可以利用友元的技术解决函数调用的问题 但是注意类外无法访问数据成员
//所以应该先给他们赋初始值

private:
int m_a;
int m_b;
};

//只能利用全局函数重载左移运算符

//点击cout,可以查看cout的数据类型
//void operator<<(ostream &cout, Person &p) {//本质 operator<< (cout , p) 简化为cout << p
// cout << "m_a=" << p.m_a << " " << "m_b=" << p.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;//直接返回,后面就不会再重载endl
//}

//另外一种写法:给out起别名,其实就是引用,指向同一块内存空间
ostream& operator<<(ostream& out, Person& p) {//本质 operator<< (cout , p) 简化为cout << p
out << "m_a=" << p.m_a << " " << "m_b=" << p.m_b;
return out;//直接返回,后面就不会再重载endl
}
void test01() {
/*Person p;
p.m_a = 10;
p.m_b = 10;*/

//修改为
Person p(10, 10);

cout << p << endl;
}

void test02() {
/*Person p1;
p1.m_a = 10;
p1.m_b = 10*/;

//修改为
Person p1(10, 10);

cout << p1;//系统提示没有与这些操作数匹配的"<<"的运算符,重载后可以运行

cout << p1 << endl;//会报错,因为后面添加了endl,解决方案在上

cout << p1 << "hello world" << endl;//没输出一次就返回cout,可以往后无限叠加
}
int main() {
//test01();
test02();
return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

4.5.3 递增运算符重载

作用: 通过重载递增运算符,实现自己的整型数据

class MyInteger {

friend ostream& operator<<(ostream& out, MyInteger myint);

public:
MyInteger() {
m_Num = 0;
}
//前置++
MyInteger& operator++() {
//先++
m_Num++;
//再返回
return *this;
}

//后置++
MyInteger operator++(int) {
//先返回
MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
m_Num++;
return temp;
}

private:
int m_Num;
};


ostream& operator<<(ostream& out, MyInteger myint) {
out << myint.m_Num;
return out;
}


//前置++ 先++ 再返回
void test01() {
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}

//后置++ 先返回 再++
void test02() {

MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}

int main() {

test01();
//test02();

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;//注意这里返回我们写的myint,this是指向自身,*是解引用的操作
}

//重载后置++运算符
//void operator++() {//z这里会报错,因为发生重定义现象
//}

//后置返回的是值,注意不能返回引用,如果返回引用,返回的是一个局部的对象,局部对象在当前函数执行完后就被释放了,再返回它就是非法操作了
MyInteger operator++(int) {//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 test01() {
MyInteger myint;
cout << myint << endl;//没有与之匹配的操作运算符
}

void test02() {
MyInteger myint1;

//返回引用的输出效果
//这种都是对同一个数据类型进行递增,也就是myint1
cout << ++myint1 << endl;
//输出为1

cout << ++(++myint1) << endl;//提示没有与之匹配的操作运算符,解决方案在上
//输出为3

cout << myint1 << endl;
//输出为3

////不返回引用的输出效果
// 返回的是一个新的对象,再对该新对象++
//cout << ++myint1 << endl;
////输出为1

//cout << ++(++myint1) << endl;//提示没有与之匹配的操作运算符,解决方案在上
////输出为3

//cout << myint1 << endl;
////输出为2



}

void test03() {
MyInteger myint2;
cout << myint2++ << endl;

cout << myint2 << endl;
}
int main() {
//test01();
test02();
test03();
return 0;
}

总结: 前置递增返回引用,后置递增返回值

4.5.4 赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

示例:

class Person
{
public:

Person(int age)
{
//将年龄数据开辟到堆区
m_Age = new int(age);
}

//重载赋值运算符
Person& operator=(Person &p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;

//提供深拷贝 解决浅拷贝的问题
m_Age = new int(*p.m_Age);

//返回自身
return *this;
}


~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}

//年龄的指针
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;
}

int main() {

test01();

//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;
}

自己写的:

#include<iostream>
using namespace std;
//赋值运算符重载
class Person {
public:
Person(int age) {
m_age = new int(age);//new开辟出来的东西需要使用指针接受,堆区数据由程序员手动开辟,手动释放
}

~Person() {//把堆区数据释放干净
//这是一个浅拷贝
//调用时能正常输出,但系统崩溃,因为p1与p2指向同一块内存,内存被重复释放,导致系统崩溃
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);
cout << p1.m_age << endl;//输出的是指针指向的地址

cout << *p1.m_age << endl;//解引用,输出p1年龄

Person p2(20);

cout << *p2.m_age << endl;//输出p2年龄

p2 = p1;//赋值操作

cout << *p2.m_age << endl;//输出年龄也是18

Person p3(30);

p3 = p2 = p1;

cout << *p3.m_age << endl;
}
int main() {
test01();

int a = 10;
int b = 20;
int c = 30;

c = b = a;
//连续赋值操作
/*
cout << a << endl;
cout << b << endl;
cout << c << endl;*/

return 0;
}

4.5.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

示例:

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;
}
else
{
return false;
}
}

bool operator!=(Person & p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return false;
}
else
{
return true;
}
}

string m_Name;
int m_Age;
};

void test01()
{
//int a = 0;
//int b = 0;

Person a("孙悟空", 18);
Person b("孙悟空", 18);

if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}

if (a != b)
{
cout << "a和b不相等" << endl;
}
else
{
cout << "a和b相等" << endl;
}
}


int main() {

test01();

system("pause");

return 0;
}

自己写的:

#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_name == p.m_name && this->m_age == p.m_age) {
return 1;
}
return 0;
}

//重载!=号
bool operator!=(Person& p) {
if (this->m_age != p.m_age || this->m_name != p.m_name) {
return 1;
}
return 0;
}


string m_name;
int m_age;
};
void test01() {
Person p1("Sam", 18);
Person p2("Sam", 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;
}
}
int main() {
test01();
return 0;
}

4.5.6 函数调用运算符重载

  • 函数调用运算符 () 也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活

示例:

class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}

};
void test01()
{
//重载的()操作符 也称为仿函数
MyPrint myFunc;
myFunc("hello world");
}


class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};

void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;

//匿名对象调用
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

test01();
test02();

system("pause");

return 0;
}

自己写的:

#include<iostream>
using namespace std;
//函数调用运算符重载

//打印输出类
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
};

void MyPrint2(string test) {
cout << test << endl;
}

void test01() {
MyPrint myPrint;
myPrint("Hello World");//()小括号重载 让对象使用重载后的小括号 由于使用起来非常类似于函数调用,因此称为仿函数

MyPrint2("Hello World");//函数的调用
}
//仿函数非常的灵活,没有固定的写法
//加法类
class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
};

void test02() {
MyAdd myadd;
int ret = myadd(100, 100);

cout << "ret=" << ret << endl;

//匿名函数对象
cout << MyAdd()(100, 100) << endl;//通过一个类名和一个()创建一个匿名对象,用完就被释放
//输出为200

}
int main() {
test01();
test02();

return 0;
}