C++从0到1的入门级教学(十二)——运算符重载

12 运算符重载

在本讲中,我们会设计到一些使得对象操作更美观的技术——运算符重载。运算符重载是一种C++多态的形式。

运算符通过重载,可以实现其原本没有的含义。C++允许运算符重载扩展到用户定义的类型,如将两个对象相加。

我们前面实际上学过数组,如果要将两个数组的对应元素相加,我们需要写一个for循环来遍历数组中的元素,并且分别进行相加。实际上,这就是一种简单的重载,我们口头上所说的arr1+arr2,实际上赋予了——加是对应元素相加的这层含义。

所以,为了简化它的意思,我们可以将对应元素相加后的新数组写为以下的形式:

arr3 = arr1+arr2

总结上述所讲:运算符重载的含义是:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

重载的通用格式如下所示:

operator op(argument - list)

op指的是C++现有的运算符;例如加法运算符重载为operator +()。当然C++以外的、虚构的运算符不可重载,这是需要注意的。

12.1 加法运算符重载

对于内置数据类型,编译器知道如何进行运算:

  • int a = 10;
  • int b = 10;
  • int c = a+b

但是我们假如是下面这种情况呢?

class Person
{
public:
	int m_A;
	int m_B;
	
}

Person p1;
p1 m_A = 10;
p1 m_B = 10;

Person p2;
Person p1;
p2 m_A = 10;
p2 m_B = 10;

Person p3 = p1+p2;

在这种情况下,C++编译器就会蒙了,Person是啥类型,怎么还能相加?

我们实际上可以编写一个成员函数来实现此功能,实现两个对象相加属性后返回新的对象。而对于C++来说,为了指明是加法运算符的作用发生了变化,故该函数统一命名为operatao +。

如果你按照它的命名规则来了,那么在调用函数时,你就可以使用p3 = p1+p2的格式去调用这个函数。

以上的方式我们叫做通过成员函数来重载+号运算符,除此之外,我们还可以通过全局函数来重载+号运算符。

让我们看看下面的例子来理解我上述的话。

#include <iostream>
using namespace std;

//加号运算符重载
class Person
{
public:
	//成员函数重载+号
	/*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;


private:

};

//全局函数重载+号
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;
}

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:" << p3.m_A << endl;
	cout << "p3.m_B:" << p3.m_B << endl;
}


int main() 
{
	test01();
}

out:

image-20220409192058008

运算符重载也可以发生函数重载。这样的原因是可以应对多种情况。如p3 = p2+10,对于10,我们并没有写出对应解决的方案函数,所以如果运算符重载不能发送函数重载,那么就对运算符不太友好了。

总的来说,加法运算符就是使得自定义的数据类型能够按照自己想要的方式相加。需要注意的是,对于内置的数据类型的操作运算符是不可改变的;并且,对于运算符的重载不要滥用。

12.2 左移运算符重载

12.2.1 演示与说明

左移运算符指的是我们在输出的时候使用的那个运算符。

cout<<a<<endl;

如果是对于内置的基本数据类型,那肯定是可以利用左移运算符来进行输出的。但是如果是对象呢?

Person p;
cout<<p<<endl;

这样的对象输出的结果肯定是不被允许的,如下所示。

image-20220409193537162

对此,我们可以通过重载左移运算符来获得我们想要的效果。

我们前面说过可以通过成员函数和全局函数来重载,但是对于左移运算符,其并不能在成员函数中被重载,因为逻辑不对。所以,对于通常的左移运算符重载,我们都是在全局函数中去实现它。

试着敲一下下面的代码你就能理会其中的思想了。

#include <iostream>
using namespace std;

class Person
{
public:
	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符
void operator<<(ostream& cout, Person& p) 
{
	cout << "m_A = " << p.m_A << "m_B = " << p.m_B ;
}

void test01() 
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 20;
	cout << p1 ;
}

int main() 
{
	test01();
	system("pause");
}

out:

image-20220409194618705

我们实际上可以尝试在重载过后的<<写上这样的代码:

cout<<p1<<endl;

很快地,你会发现报错了。报错的原因是因为,你的重载函数并没有返回值,而对于cout之所以可以使用两个左移运算符,是因为其内含有链式编程的思想。故,如果想要使得可以连续使用<<,我们必须将函数的返回值改为cout即可。

顺便提一句,cout是ostream实例化的结果,即输出对象。

ostream对象?

ostream对象是指输出流对象,通常来说,cout输出的对象是指显示器,当然,还有输入流对象ofstream,这些我们到后面再去提及。

我们将以上的代码改写为如下即可实现多次使用<<:

#include <iostream>
using namespace std;

class Person
{
public:
	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 p1;
	p1.m_A = 10;
	p1.m_B = 20;
	cout << p1 << endl;
}

int main() 
{
	test01();
	system("pause");
}

out:

image-20220409195328261

为什么使用引用?

在上述的代码中实际上两个位置使用到了引用。返回值和参数是引用的原因是:我们自始至终都想这对cout这个对象做功能上的修改,而不是从ostream再创建一个cout对象,然后赋予它新功能。故,我们要加上引用。

12.2.2 常见的友元使用:重载>>运算符

在实际开发中,我们实际上会把成员属性写入私有,这样的话,对于重载函数无法访问,我们只需要利用友元即可解决这个问题。

#include <iostream>
using namespace std;

class Person 
{
	friend ostream& operator<<(ostream& cout, Person& p);
private:
	int m_A =10;
	int m_B = 20;
};

//重载左移运算符
ostream& operator<<(ostream& cout, Person& p) 
{
	cout << "m_A = " << p.m_A << "m_B = " << p.m_B;
	return cout;
}

void test01() 
{
	Person p1;
	cout << p1 << endl;
}

int main() 
{
	test01();
}

out:

image-20220415115324196

12.3 递增运算符重载

我们来看看递增运算符的原理。

int a = 10;

cout<< ++a <<endl;//11
cout << a <<endl; //结果为11

cout <<b++<<endl;//10
cout << b <<endl;//11

对于++a,其原理为先做++操作再输出,而对于b++,其原理为先输出再++。

同样地,我们对对象做自增操作,其原理如下:

class MyInteger
{
public:
	MyInteger()
	{
		m_Num = 0;
	}
	
private:
	int m_Num;
}

MyInteger myint;
cout <<myint<<endl; //0
cout << ++myint <<endl; //1
cout << myint++ <<endl; //1
cout << myint <<endl; //2

我们来试着实现上述的原理:

#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) //不能返回引用,因为此时返回值是局部变量
	 {
		//1 记录当前结果
		 MyInteger temp = *this;
		//2 递增
		 m_Num++;
		//3 将记录结果返回
		 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 mying;
	cout << mying++ << endl;
	cout << mying << endl;
}

int main() 
{
	test01();
	test02();
	system("pause");
}

12.4 赋值运算符重载

赋值运算符的重载原理涉及到深浅拷贝,我们从代码中理解比较好。

#include <iostream>
using namespace std;

//赋值运算符重载
class Person
{
public:
	Person(int age) 
	{
		m_Age = new int(age);
	}
	~Person() 
	{
		if (m_Age != NULL) 
		{
			delete(m_Age);
			m_Age = NULL;
		}
	}

	int* m_Age;
};

void test01() 
{
	Person p1(18);
	Person p2(20);
	p2 = p1;//赋值操作

	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;

}

int main() 
{
	test01();
	system("pause");
	return 0;
}

从以上的代码中,我们会发现无法运行,根本原因是因为:

image-20220410202023036

也就是说,根本原因是因为原来的赋值运算符提供的是一个浅拷贝的过程,但是我们想要的是一个深拷贝的效果,故,我们需要重载赋值运算符。如下所示:

#include <iostream>
using namespace std;

//赋值运算符重载
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) 
	{
		//对于编译器来说,其提供的是浅拷贝
		//我们应该需要用深拷贝

		//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
		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;
}

int main() 
{
	test01();
	system("pause");
	return 0;
}

out:

image-20220410203741425

12.5 关系运算符重载

对于自定义的数据类型,C++是无法提供对比方式的,如>、<、=等。为此,我们需要重载关系运算符。

让我们用以下的代码来解决这个问题:

#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 true;
		}
		return false;
	}

	
	
	string m_Name;
	int m_Age;

private:

};

void test01() 
{
	Person p1("Tom", 18);
	Person p2("jarry", 19);
	//Person p2("Tom", 18);

	if (p1 == p2) 
	{
		cout << "相同" << endl;
	}
	else
	{
		cout << "不相同" << endl;
	}
}

int main() 
{
	test01();
}

12.6 函数调用运算符重载

函数调用运算符()也可以重载,需要注意的是,由于重载后使用的方式非常像函数的调用,因此称为仿函数。仿函数没有固定的写法,其风格多变。

让我们试着敲一下下面的代码:

#include <iostream>
using namespace std;
#include <string>

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

class MyAdd
{
public:
	//重载例子2
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};


void test01() 
{
	MyPrint myPrint;
	myPrint("hello world");
}

void test02() 
{
	MyAdd myadd;
	cout << "ret = " << myadd(100, 200) << endl;
}

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

C++和Java一样,也是创建一个匿名对象,匿名对象使用后当场销毁。如在上例中我想创建一个匿名对象,只需:

MyAdd()(100,100)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ArimaMisaki

如果知识有用请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值