【C++】重载运算符operator

运算符重载概念:

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

语法:

<返回类型> operator <运算符符号>(<参数列表>)
{
        <函数体>
}

注意:有五个运算符不能重载 .   *    ::    sizeof    ?: . 

重载的运算符

加号运算符 +

1、作为成员函数重载运算符

2、作为全局函数重载运算符(当访问类内私有成员时,可以利用友元解决)

3、运算符重载 也可以发生函数重载

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 &pl, Person &p2)
//{
//	Person temp;
//	temp.m_A = pl.m_A + p2.m_A;
//	temp.m_B = pl.m_B + p2.m_B;
//	return temp;
//}
//3、运算符重载 也可以发生函数重载
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 test()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 20;
	Person p2;
	p2.m_A = 11;
	p2.m_B = 22;
	//成员函数重载本质调用
	//Person p3 = p1.operator+(p2);
	//全局函数重载本质调用
	//Person p3 = operator+(p1, p2);
	
	//简化后写法
	Person p3 = p1 + p2;
	cout <<"p3.m_A :"<<p3.m_A << endl;
	cout << "p3.m_B:" << p3.m_B << endl;
	
	//函数重载
	int num = 30;
	Person p4 = p1 + num;
	cout << "函数重载版本" << endl;
	cout << "p4.m_A :" << p4.m_A << endl;
	cout << "p4.m_B:" << p4.m_B << endl;
}

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的。——比如:1+1=3是不行的。

总结2:不要滥用运算符重载——比如:不要重载加号实现减法内容。

左移运算符 <<

为了实现cout在左边,只能利用全局函数重载左移运算符
cout 是ostream类型的对象
为了实现连续调用<<,重载运算符函数返回值类型要是ostream&

示例:
 

class Person
{
public:
	//利用成员函数重载 左移运算符    p.operator<<(cout)  简化版本 p << cout
	//不会利用成员函数重载<<运算符,因为无法实现 cout在左侧
	//void operator<<(cout)
	//{
	//
	//}
	int m_A;
	int m_B;
};
//只能利用全局函数重载左移运算符
//本质是  operator<<(cout,p)   简化后cout<<p
//要想实现一次<<后,还能在其后面继续实现<<,则需要将函数返回值类型改为 ostream &
ostream & operator<<(ostream& cout, Person& p)
{
	cout << "p.m_A:" << p.m_A << "p.m_B:" << p.m_B;
	return cout;
}
void test()
{
	Person p;
	p.m_A = 10;
	p.m_B = 20;
	cout << p << endl;
}

递增运算符  ++

前置递增++a,先运算后执行表达式
后置递增a++,先执行表达式后运算

int a=0;int b=a++;//a=1,b=0
int a=0;int b=++a;//a=1,b=1

示例:

class MyInteger
{
	friend ostream& operator<<(ostream &cout, MyInteger myint);
	////全局函数类外重载++
	//friend MyInteger& operator++(MyInteger &myint);
	//friend MyInteger operator++(MyInteger &myint, int);
public:
	MyInteger()
	{
		this->m_Num = 10;
	}
	//1、成员函数重载前置递增 ++a
	MyInteger& operator++()		//返回类型是MyInteger&,链式编程,要加&返回给对象本身
	{
		this->m_Num++;
		return *this;			//解引用this指针,返回该对象数据
	}

	//2、成员函数重载后置递增 a++
	//void operator++(int) int代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int)	//int变量为占位参数,区分前后置递增
	//不需要返回引用,因为返回的是个局部变量。因此也不能实现链式编程
	{	
		MyInteger temp = *this;
		this->m_Num++;
		return temp;
	}
private:
	int m_Num;
};

////前置递增  ++a 类外实现
//MyInteger& operator++(MyInteger &myint)	//本质调用为operator++(myint)
////返回值类型是 MyInteger& 可以实现链式编程,若无&,则返回到一个新的数据中,不会返回到myint这个对象
//{
//	myint.m_Num++;
//	return myint;						//返回myint这个数据
//}
//
////后置递增 a++ 类外实现
//MyInteger operator++(MyInteger &myint, int)
//{
//	MyInteger temp = myint;
//	myint.m_Num++;
//	return temp;
//}

//自定义左移运算
ostream& operator<<(ostream &cout, MyInteger myint)
{
	cout << myint.m_Num;
	return cout;
}
void test()
{
	MyInteger myint;
	
	cout << ++(++myint) <<endl;		//输出12
	cout << myint << endl;			//输出12
	cout << myint++ <<endl;			//输出12
	cout << myint<<endl;			//输出13
}

赋值运算符 =

重载赋值运算符=,可以解决浅拷贝问题

class Person
{
public:
	Person(int age)
	{
		this->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 test()
{
	Person p1(10);
	Person p2(20);
	//p1 = p2;		//没有重载 = 时,*m_Age共用同一个开辟空间,析构函数重复释放内存
	Person p3(30);
	p1 = p2 = p3;
	cout << "*p1.m_Age:" << *p1.m_Age << endl;
	cout << "*p1.m_Age:" << *p1.m_Age << endl;
	cout << "*p1.m_Age:" << *p1.m_Age << endl;
}

关系运算符 ==  >  <

比较简单,和加号运算符重载类似,但是返回布尔型 bool

函数调用运算符  ()

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

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

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

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void myprint(string text)
{
	cout << text << endl;
}
void test01()
{
	MyPrint mp;
	mp("hello world");			//仿函数,使用起来类似函数调用
	myprint("hello world");		//函数
}

//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
	int operator()(int num1,int num2)
	{
		return num1 + num2;
	}
};
void test02()
{
	MyAdd myadd;
	int ret = myadd(10, 20);
	cout << "ret:" << ret << endl;
	//匿名函数对象
	cout << MyAdd()(30, 40) << endl;
}

注意:可以使用匿名函数对象调用,该对象没有名字,用完即释放

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值