C++类和对象——运算符重载详解

目录

1.运算符重载概念

2.加号运算符重载

 通过全局函数重载

代码示例:

3.左移运算符重载

代码示例:

4.递增运算符重载

代码示例:

5.赋值运算符重载 

深拷贝

代码示例: 

6.关系运算符重载 

代码示例: 

7.函数调用运算符重载

匿名函数对象


1.运算符重载概念

2.加号运算符重载

 

比方说,有一个名称为person的类,里面有m_A和m_B这两个成员

我们想实现它们相加的操作,可以写这样一个函数

 

我们只需要将函数名替换为operator+

就可以对加号运算符进行重载,简化代码 

 

也可以通过全局函数重载

 通过全局函数重载

 

代码示例:

#include<bits/stdc++.h>
using namespace std;

class person
{
public:
	
	//成员函数重载+运算符
	// person operator+(person &p)
	// {
		// person temp;
		// temp.a = this -> a + p.a;
		// temp.b = this -> b + p.b;
		// return temp;
	// }
	
	int a;
	int b;
};

//全局函数重载加号运算符
person operator+(person &p1,person &p2)
{
	person temp;
	temp.a = p1.a + p2.a;
	temp.b = p1.b + p2.b;
	return temp;
}

int main()
{
	person p1;
	p1.a = 5;
	p1.b = 10;
	
	person p2;
	p2.a = 3;
	p2.b = 9;
	
	person p3 = p1 + p2;
	
	cout << p3.a << endl;
	cout << p3.b << endl;
	return 0;
}

 

3.左移运算符重载

 如果要输出私有成员可以配合友元

只能用全局函数进行重载

cout是标准输出流对象,ostream 

代码示例:

#include<bits/stdc++.h>
using namespace std;

class person
{
public:

//成员函数重载
	int a;
	int b;
};

//全局函数重载
ostream & operator<<(ostream &cout,person &p){
	cout << p.a << endl;
	cout << p.b << endl;
	return cout;
}

int main()
{
	person p;
	p.a = 5;
	p.b = 10;

	cout << p << endl;
	return 0;
}

 

4.递增运算符重载

cout << ++a是先让a加一,再执行这个表达式

cout << a++是先执行这个表达式,再让a加一

代码示例:

#include<bits/stdc++.h>
using namespace std;

class integer
{
public:
	
	integer(){
		num = 0;
	}
	//重载前置++运算符
    //返回&引用是为了一直对一个数据进行递增操作
	integer & operator++()
	{
		num++;
		return *this;
	}
	
    //重载后置++运算符
    //int是一个占位参数
    //可以用于区分前置和后置递增
	integer operator++(int)
	{
		integer temp = *this;
		num++;
		return temp;
	}
	
	int num;
};

//后面的i前面不用&,不然写in++的时候会报错
//因为返回的是temp,temp是局部变量
ostream & operator<<(ostream &cout,integer i){
	cout << i.num << endl;
	return cout;
}

int main(){
	integer i;
	cout << ++(++i) << endl;
	
	integer in;
	cout << in++ << endl;
	cout << in << endl;
	
	return 0;
}

 

5.赋值运算符重载 

深拷贝

这里涉及到深浅拷贝的问题

不懂的可以看http://t.csdnimg.cn/FHNI4 

 

代码示例: 

#include<bits/stdc++.h>
using namespace std;

class person
{
public:

	person(int a)
	{
		age = new int(a);
	}
	
	~person()
	{
		if(age != NULL)
		{
			delete age;
			age = NULL;
		}
	}
	
	//返回引用才是它真正的自身
	person& operator=(person &p)
	{
		//编译器提供的是浅拷贝
		//age = p.age;
		
		//应该先判断是否有属性在堆区
		//如果有,先释放干净
		//再进行深拷贝
		if(age != NULL)
		{
			delete age;
			age = NULL;
		}
		
		age = new int(*p.age);
		
		return *this;
	}
	
	int *age;
};

int main()
{
	person p1(18);
	person p2(20);
	person p3(22);
	
	p3 = p2 = p1;
	
	cout << *p1.age << endl;
	cout << *p2.age << endl;
	cout << *p3.age << endl;
	
	return 0;
}

 

6.关系运算符重载 

代码示例: 

#include<bits/stdc++.h>
using namespace std;

class person
{
public:

	person(string name,int age)
	{
		this -> name = name;
		this -> age = age;
	}
	
	bool operator==(person &p)
	{
		if(this -> name == p.name && this -> age == p.age)
			return true;
		else
			return false;
	}
	string name;
	int age;
};

int main()
{
	person p1("xiaoming",18);
	person p2("xiaoming",18);
	
	if(p1 == p1)
	{
		cout << "p1与p2相等" << endl;
	}
	
	return 0;
}

 

7.函数调用运算符重载

匿名函数对象

特点为:当前行执行完后立即被释放 

int main()
{
	test p;
	
	p("hello world");
	p(5,6);
	
	//匿名函数对象
	test()("hello world !!!");
	test()(7,9);
	
	return 0;
}

代码示例: 

#include<bits/stdc++.h>
using namespace std;

class test
{
public:

	void operator()(string test)
	{
		cout << test << endl;
	}
	
	void operator()(int a,int b){
		cout << a+b << endl;
	}
	
};

int main()
{
	test p;
	
	p("hello world");
	p(5,6);
	
	//匿名函数对象
	test()("hello world !!!");
	test()(7,9);
	
	return 0;
}

 

  • 28
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

柏箱

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值