这篇来学习一下运算符重载,运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。这些运算符重载,包括,加减号,左右移,关系运算符,赋值运算符和递增递减运算符。
1.加号运算符重载
例如,两个自定义类对象相加,怎么操作?
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
};
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();
system("pause");
return 0;
}
上面代码在Person p3 = p1+ p2的时候就会报错,但是我们就是想实现这样加号的功能,的出来p3中m_A和m_B的值是p1和p2下m_B和m_A相加的结果。
这种需求就需要使用C++中的加号运算符重载方式实现,在C++中,要实现加号运算符重载,有两个方法,第一种就是在类内部写成员函数实现,第二种全局函数实现,两个方法都有一个功能的函数名称operator+(), 这个函数名称是固定,不能写其他,不然编译器就不知道我们在实现加号运算符的重载。
下面我们先来看看第一种方法,类的成员函数实现operator+()
2.成员函数实现加号运算符重载
类的成员函数实现operator+(),函数参数有至少一个参数,例如Person &p, 函数的返回值是Person类型,那么这个函数的代码就是下面这样。
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
//加号运算符重载实现方式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;
}
};
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();
system("pause");
return 0;
}
运行结果:
分析这个加号运算符重载,函数里面实现的逻辑,很简单,就是两个int类型的变量的相加,返回一个Person对象。
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; // 等价于下面这行代码
Person p3 = p1.operator+(p2);
cout << "p3的m_A的值为"<< p3.m_A << endl;
cout << "p3的m_B的值为" << p3.m_B << endl;
}
本质上加号运算符重载功能是上面代码第二种方式也就是 Person p3 = p1.operator+(p2);
但是为了简便,我们调用的时候不写函数operator+(), 可以直接简写为:Person p3 = p1 + p2;
成员函数实现加法运算符重载,我们再来看看全局函数实现加号运算符重载的代码
3.全局函数实现加号运算符重载
有了前面代码基础和理解,直接写一个全局函数的实现。
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
//加号运算符重载实现方式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;
}
};
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; // 等价于下面这行代码
Person p3 = operator+(p1, p2);
cout << "p3的m_A的值为"<< p3.m_A << endl;
cout << "p3的m_B的值为" << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
这个时候全局函数operator+(Person &p1, Person &p2) , 有两个参数,实现相加的逻辑是一样的。调用的时候,有两种方法,为了简单,我们都习惯简写成 p3 = p1 + p2的代码形式。
4.加号运算符函数重载
前面基础知识学习过函数的重载,就是函数名相同,函数形式参数和个数和类型不同。编译器会根据调用的时候参数个数和类型,自动去调用函数,这就是函数重载。通过上面全局函数和成员函数的例子,我们看到了一点函数重载的例子,虽然一个函数在类内部,一个函数在类外部(全局函数),这样在写一个全局函数来实现加号运算符的重载。
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
//加号运算符重载实现方式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;
}
};
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 x)
{
Person temp;
temp.m_A = p1.m_A + x;
temp.m_B = p1.m_B + x;
return temp;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
//运算符函数重载
Person p3 = p1 + 15;
cout << "p3的m_A的值为"<< p3.m_A << endl;
cout << "p3的m_B的值为" << p3.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
关键看代码 p3 = p1 + 10, 第二个参数是int类型的10,而不是一个Person对象,显然这里发生重载的函数就是
Person operator+(Person &p1, int x)
{
Person temp;
temp.m_A = p1.m_A + x;
temp.m_B = p1.m_B + x;
return temp;
}
通过这个加号的运算符重载的例子,我们应该可以推理出,其他三个四则运算的减法,乘法和除法都是这个逻辑,这个运算符重载就通过学习加号就可以。