黑马程序员c++ 运算符重载

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

加号运算符重载

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

本质:运算符重载本质上就是函数的调用。

实现方法:对于函数来说,对两个自定义数据类型进行运算可以调用类内函数也可以调用全局函数,那么运算符重载也可以有类内实现和全局实现两个实现方法。

首先介绍类内实现

#include <iostream>
using namespace std;

class Person
{
public:
	
	int m_A;
	int m_B;
	
	
	Person (int a, int b)
	{
		m_A = a;
		m_B = b;
	}
//	Person PersonAddPerson(const Person &p1)
//	{
//		Person p;
//		p.m_A = this -> m_A + p.m_A;
//		p.m_B = this -> m_B + p.m_B;
//		return p;
//	}

	Person operator+(const Person &p1)
	{
		Person p(m_A + p1.m_A, m_B + p1.m_B);
		return p;
	}
};

int main()
{
	int a = 10;
	Person p1(10, 20);
	Person p2(20, 30);
	
	Person p3 = p1 + p2;

	cout << "p3.a = " << p3.m_A << " p3.b = " << p3.m_B << endl;
	
	return 0;
}

对于两个自定义数据类型进行相加,我们可以在类内写一个函数,PersonAddPerson,调用时就可以写成 Person p3 = p1.PersonAddPerson(p2),如果利用加号运算符重载,我们只需要把类内成员函数名改为operator+即可,这样在调用时就可以写成Person p3 = p1 + p2。

同样的对于全局实现方法:

#include <iostream>
using namespace std;

class Person
{
public:
	
	int m_A;
	int m_B;
	
	
	Person (int a, int b)
	{
		m_A = a;
		m_B = b;
	}
//	Person PersonAddPerson(const Person &p1)
//	{
//		Person p;
//		p.m_A = this -> m_A + p.m_A;
//		p.m_B = this -> m_B + p.m_B;
//		return p;
//	}

//	Person operator+(const Person &p1)
//	{
//		Person p(m_A + p1.m_A, m_B + p1.m_B);
//		return p;
//	}
};

Person operator+(const Person &p1, const Person &p2)
{
	Person p3(p1.m_A + p2.m_A, p1.m_B + p2.m_B);
	return p3;
}

int main()
{
	int a = 10;
	Person p1(10, 20);
	Person p2(20, 30);
	
	Person p3 = p1 + p2;
	
	cout << "p3.a = " << p3.m_A << " p3.b = " << p3.m_B << endl;
	
	return 0;
}

原本的调用全局函数实现两个Person对象相加的写法为Person p3 = PersonAddPerson(p1, p2),替换函数名发生加号运算符重载之后就变为Person p3 = p1 + p2;

除了加号运算符的实现,我们还要注意到,因为运算符重载本质上就是将函数的名字变了,所以运算符重载也是可以发生函数重载的。

#include <iostream>
using namespace std;

class Person
{
public:
	
	int m_A;
	int m_B;
	
	
	Person (int a, int b)
	{
		m_A = a;
		m_B = b;
	}
//	Person PersonAddPerson(const Person &p1)
//	{
//		Person p;
//		p.m_A = this -> m_A + p.m_A;
//		p.m_B = this -> m_B + p.m_B;
//		return p;
//	}

//	Person operator+(const Person &p1)
//	{
//		Person p(m_A + p1.m_A, m_B + p1.m_B);
//		return p;
//	}
};

Person operator+(const Person &p1, const Person &p2)
{
	Person p3(p1.m_A + p2.m_A, p1.m_B + p2.m_B);
	return p3;
}

int operator+(const Person &p, const int &a)
{
	return p.m_A + p.m_B + a;
}

Person operator+(const Person &p1, int &a)
{
	Person p(p1.m_A + a, p1.m_B + a);
	return p;
}

int main()
{
	int a = 10;
	Person p1(10, 20);
	Person p2(20, 30);
	
	Person p3 = p1 + p2;
	Person p4 = p1 + a;
//	Person p3 = p1.operator+(p2);
	
	cout << "p3.a = " << p3.m_A << " p3.b = " << p3.m_B << endl;
	cout << "p + 10 = " << p3 + 10 << endl;
	cout << "p4.a = " << p4.m_A << " p4.b = " << p4.m_B << endl;
	
	return 0;
}

在上面的代码中,利用了const可以对引用的参数发生函数重载,实现了另外两种运算符重载,其中operator+(const Person &p, int &a)接受自定义变量Person和int类型的变量,返回值为Person类型,而operator(const Person &p, const int &a)接受自定义变量Person和int类型的常量或者const修饰的变量,返回值为int类型。

总结1:对于内置的数据类型表达式的运算符是不能发生运算符重载的,运算符重载只能发生在自定义数据类型中。

例如1+1加号的含义就是两个int类型相加,不能改变其含义

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

因为运算符重载可以改变运算符的含义,那么如果operator+函数中出现了奇怪的操作就会降低程序的可读性。

左移运算符重载

我们可以对加号进行运算符重载,同样的我们也可以对左移运算符"<<"进行重载实现自定义数据类型的输出。

在实现之前我们首先要知道cout运算符本质上是ostream(输出流类)实例化的一个对象,并且一个程序只能有该类的一个对象。

那么我们先看看类内如何实现

在加号运算符重载中,我们构造的类内p1的重载函数为Person operator+(Person p),实现的操作为p3 = p1 + p2;,同样带入左移运算符"<<",要实现cout << p,如果写成Person operator<<(ostream &cout)(因为一个程序中只能有一个cout所以我们用引用传递),最后结果就会变为p << cout;

#include <iostream>
using namespace std;

class Person
{
public:
	
	int m_A;
	int m_B;
	
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	
	ostream& operator<<(ostream& cout)
	{
		cout << " a = " << m_A << " b = " << m_B;
		return cout;
	}
};

int main()
{
	Person p1(10, 20);
	Person p2(20, 30);
	
	(p2 << (p1 << cout)) << endl;
	
	return 0;
}

<<重载为一个函数之后会有返回值,这里将返回值设为cout的类型ostream的引用,一是因为我们要不断使用cout输出,在执行完p1 << cout之后整个表达式就变成了(p2 << cout) << endl;,二是因为一个程序中只能有一个cout所以返回cout的引用。

因为操作符是从左到右进行的所以这里加了括号保证执行的顺序。

很明显上面的代码不能实现我们想要的结果,这时我们再来看看全局函数能不能实现

在加号运算符重载中operator(Person p1, Person p2),变成了p1 + p2;,那么在左移运算符重载中,我们只要写成operator(ostream &cout, Person p)即可。

#include <iostream>
using namespace std;

class Person
{
public:
	
	int m_A;
	int m_B;
	
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	
	ostream& operator<<(ostream& cout)
	{
		cout << " a = " << m_A << " b = " << m_B;
		return cout;
	}
};

ostream& operator<<(ostream& cout, const Person& p)
{
	cout << " a = " << p.m_A << " b = " << p.m_B;
	return cout;
}

int main()
{
	Person p1(10, 20);
	Person p2(20, 30);
	
	(p2 << (p1 << cout)) << endl;
	cout << p1 << p2 << endl;
	
	return 0;
}

需要注意的是因为在输出流中我们要不断地用cout使用<<运算符,所以我们<<运算的返回值也应该是cout,这也是利用了链式编程的思想。

还有一点就是:在类中我们常常将成员变量设置为私有,这时我们如何在全局函数中输出成员变量呢,

第一种方法是利用共有的成员函数接口来访问成员变量。

#include <iostream>
using namespace std;

class Person
{
private:
	
	int m_A;
	int m_B;
	
public:
	
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	
	ostream& operator<<(ostream& cout)
	{
		cout << " a = " << m_A << " b = " << m_B;
		return cout;
	}
	
	int geta()
	{
		return m_A;
	}
	
	int getb()
	{
		return m_B;
	}
};

ostream& operator<<(ostream& cout, const Person& p)
{
//	cout << " a = " << p.m_A << " b = " << p.m_B;
	cout << " a = " << p.geta() << " b = " << p.getb();
	return cout;
}

int main()
{
	Person p1(10, 20);
	Person p2(20, 30);
	
	(p2 << (p1 << cout)) << endl;
	cout << p1 << p2 << endl;
	
	return 0;
}

但更简单的方法是我们可以直接将运算符重载函数设置为友元类,来访问私有的成员变量。

#include <iostream>
using namespace std;

class Person
{
	friend ostream& operator<<(ostream& cout, Person p);
private:
	
	int m_A;
	int m_B;
	
public:
	
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	
	ostream& operator<<(ostream& cout)
	{
		cout << " a = " << m_A << " b = " << m_B;
		return cout;
	}
	
	int geta()
	{
		return m_A;
	}
	
	int getb()
	{
		return m_B;
	}
};

ostream& operator<<(ostream& cout, const Person& p)
{
	cout << " a = " << p.m_A << " b = " << p.m_B;
//	cout << " a = " << p.geta() << " b = " << p.getb();
	return cout;
}

int main()
{
	Person p1(10, 20);
	Person p2(20, 30);
	
	(p2 << (p1 << cout)) << endl;
	cout << p1 << p2 << endl;
	
	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;
	}
	
	const MyInteger operator++(int) //后置运算符重载
	{
		const MyInteger temp = *this;
		m_Num++;
		return temp;
	}
	
private:
	int m_Num;
	
};

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

void text()
{
	MyInteger myint1;
	
	cout << myint1 << endl;
	cout << ++myint1 << endl;
	cout << ++myint1 << endl;
	cout << myint1 << endl;
	
	cout << endl;
	
	MyInteger myint2;
	
	cout << myint2 << endl;
	cout << myint2++ << endl;
	cout << myint2++ << endl;
	cout << myint2 << endl;
	
	cout << endl;
}

int main()
{
	text();
	
	return 0;
}

从上面的代码中有三点我们需要知道。

首先 在c++语法中,前置运算符和后置运算符重载的函数参数列表是不一样的,后置运算符相较前置运算符需要增加一个int占位符

其次前置运算符是可以实现连加的,如++(++a),对于这种链式思想,我们就要运用链式编程,就是在运算之后在返回这个对象的引用,来实现对一个对象一直进行运算。        如果不返回引用而只是返回一个值,那么在以后的连加中只是对返回的新的对象进行运算,而初始的对象的值只会保留第一次自增的结果。

最后对于后置运算符,后置运算符是不可以再接其他自增运算符的,如(a++)++,或者++(a++),就会报错

 至于原因,是因为在c++中,后置的++运算符函数会申请一个常量存储自增之前的值,再将这个常量返回,而常量自然不能被赋值,也就不能作为左值了。        所以在我们自己写的重载函数中,也要把后置运算符的返回值设置成常量。

接下来看重载函数在全局中的实现,我们以自减运算符为例。

#include <iostream>

using namespace std; 

class MyInteger
{
	
friend ostream& operator<<(ostream& cout, MyInteger myint);
friend MyInteger& operator--(MyInteger& myint);
friend const MyInteger operator--(MyInteger& myint, int);

public:
	MyInteger()
	{
		m_Num = 0;
	}
	
	MyInteger& operator++()
	{
		m_Num++;
		return *this;
	}
	
	const MyInteger operator++(int)
	{
		const MyInteger temp = *this;
		m_Num++;
		return temp;
	}
	
private:
	int m_Num;
	
};

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

MyInteger& operator--(MyInteger& myint)
{
	myint.m_Num--;
	return myint;
}

const MyInteger operator--(MyInteger& myint, int)
{
	MyInteger temp = myint;
	myint.m_Num--;
	return temp;
}

void text()
{
	MyInteger myint1;
	
	cout << myint1 << endl;
	cout << ++myint1 << endl;
	cout << ++myint1 << endl;
	cout << myint1 << endl;
	
	cout << endl;
	
	cout << --myint1 << endl;
	cout << --myint1 << endl;
	cout << myint1 << endl;
	
	cout << endl;
	
	MyInteger myint2;
	
	cout << myint2 << endl;
	cout << myint2++ << endl;
	cout << myint2++ << endl;
	cout << myint2 << endl;
	
	cout << endl;
	
	cout << myint2-- << endl;
	cout << myint2-- << endl;
	cout << myint2 << endl;
	
	cout << endl;
}

int main()
{
	text();
	
	return 0;
}

同样的,对于前置运算符我们将类型引用作为返回值,后置运算符我们将类型常量作为返回值。

需要注意的是,在全局函数的参数列表中我们要额外加入参与运算的对象,后置运算符的参数列表中要将占位符放在对象之后,在全局作用域下的运算符函数要在设置为类的友元,以访问类中的元素。

赋值运算符重载

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

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

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

例如下面的代码

#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 text()
{
	Person p1(18);
	Person p2(20);

    p2 = p1;
	
	cout << "p2.age = " << *p2.m_Age << endl;
}

int main()
{
	text();
	
	return 0;
}

在上面的代码中,将p1赋值给了p2,这时用默认的赋值运算符就会将p1中m_Age逐字节赋值给p2,所以p1,p2的m_Age就指向了同一块内存,而text函数结束时,p1执行析构函数将p1.m_Age指向的空间释放,p2在执行析构函数时再将p2.m_Age指向的空间释放就会造成空间的重复释放,编译器就会报错。

为了避免这种问题,我们就需要将默认的赋值运算符进行重载,将赋值的浅拷贝变为深拷贝。

#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=(const Person& p)
	{
		if(m_Age != NULL)
			delete m_Age;
			
		m_Age = new int(*p.m_Age);
		
		return *this;
	}
	
	int *m_Age;
};

void text()
{
	Person p1(18);
	Person p2(20);
	Person p3(30);
	
	p2 = p1;
	
	cout << "p2.age = " << *p2.m_Age << endl;
	
	p1 = p2 = p3;
	
	cout << "p1.age = " << *p1.m_Age << endl;
	cout << "p2.age = " << *p2.m_Age << endl;
	cout << "p3.age = " << *p3.m_Age << endl;
}

int main()
{
	text();
	
	return 0;
}

在我们自己的赋值运算符函数中,会先判断m_Age是否有指向,如果有就释放所指向的空间,在重新分配一块空间并且赋值。

在函数返回值方面,

如果引用返回,不用写拷贝构造函数

如果要值返回,要写拷贝构造函数 

原因:值返回时,会返回一个新的对象调用拷贝构造函数。如果不写拷贝构造函数,调用c++的拷贝函数还是会发生浅拷贝问题。

下面是值返回的代码:

#include <iostream>

using namespace std;

class Person
{
	
public:
	
	Person(int age)
	{
		cout << "有参构造函数调用" << endl;
		
		m_Age = new int(age);
	}
	
	Person(const Person& p)
	{
		cout << "拷贝构造函数调用" << endl; 
		
		if(m_Age != NULL)
			delete m_Age;
			
		m_Age = new int(*p.m_Age);
	}
	
	~Person()
	{
		if(m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	
	Person operator=(const Person& p)
	{	
		if(m_Age != NULL)
			delete m_Age;
			
		m_Age = new int(*p.m_Age);
		
		return *this;
	}
	
	int *m_Age = NULL;
};

void text()
{
	Person p1(18);
	Person p2(20);
	Person p3 = p1;
	
	p2 = p1;
	
	cout << "p3.age = " << *p3.m_Age << endl;
	
	p1 = p2 = p3;
	
	cout << "p1.age = " << *p1.m_Age << endl;
	cout << "p2.age = " << *p2.m_Age << endl;
	cout << "p3.age = " << *p3.m_Age << endl;
}

int main()
{
	text();
	
	return 0;
}

 下面是运行结果:

 需要注意的是,Person p3 = p1语句是p3对象的初始化,这里的赋值号会调用拷贝函数

 而 p2 = p1 是赋值语句,调用赋值运算符重载函数

关系运算符重载

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

如==,!=,>,<,>=,<=

下面以==和!=为例看关系运算符重载的代码。

#include <iostream>

using namespace std;

class Person
{

friend bool operator!=(const Person& p1, const Person& p2);

public:

	Person(string name, int age)
	{
		m_Name = name;
		m_Age = age;
	}
	
	bool operator==(const Person& p)
	{
		return this -> m_Name == p.m_Name && this -> m_Age == p.m_Age;
	}
	
private:
	
	string m_Name;
	int m_Age;
}; 

bool operator!=(const Person& p1, const Person& p2)
{
	return p1.m_Name != p2.m_Name || p1.m_Age != p2.m_Age;
}

void text()
{
	Person p1("张三", 18);
	Person p2("张三", 20);
	
	if(p1 == p2)
		cout << "p1等于p2" << endl;
	else
		cout << "p1不等于p2" << endl;
	
	Person p3("李四", 18);
	Person p4("李四", 18);
	
	if(p3 != p4)
		cout << "p3不等于p4" << endl;
	else
		cout << "p3等于p4" << endl; 
}

int main()
{
	text(); 
	
	return 0;
}

其中==写于类内,!=写于全局作用域。这里没有需要特别注意之处,不多赘述。

函数调用重载 小括号重载

函数调用运算符()也可以重载

由于重载后使用的方式非常像函数的调用,因此称之为仿函数。

仿函数没有固定写法,非常灵活。

例如

#include <iostream>

using namespace std;

class MyFunc
{

public:
	
	void operator()(string str)
	{
		cout << str << endl;
	}
	
	int operator()(int a, int b)
	{
		return a + b;
	}

};

void text()
{
	MyFunc myfunc;
	
	myfunc("hello, world");
	cout << myfunc(10, 10) << endl; 
}

int main()
{
	text();
	
	return 0;
}

可以看见括号运算符重载可以帮我们实现打印和加和之类的操作,非常类似于函数。

二者也有很大的区别,括号运算符重载需要我们实例化对象,调用对象的重载函数。

#include <iostream>

using namespace std;

class MyFunc
{

public:
	
	void operator()(string str)
	{
		cout << str << endl;
	}
	
	int operator()(int a, int b)
	{
		return a + b;
	}

};

//int operator()(int a, int b)
//{
//	return a + b;
//}
//'int operator()(int, int)' must be a nonstatic member function

void text()
{
	MyFunc myfunc;
	
	myfunc("hello, world");
	cout << myfunc(10, 10) << endl; 
	
	cout << MyFunc()(20, 20) << endl;
	
//	cout << (30, 30) << endl;
}

int main()
{
	text();
	
	return 0;
}

当然我们也可以使用匿名对象MyFunc(),省略对象的命名,直接调用匿名对象的重载函数。

需要注意的是,括号运算符只能用在类内作用域中,不能在全局作用域下使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值