IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++笔记 06 (运算符重载) -> 正文阅读

[C++知识库]C++笔记 06 (运算符重载)

一. 运算符重载

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

1. 加号运算符重载

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

class Person
{
	public:
		Person(){};
		Person(int a,int b)
		{
			this->m_A=a;
			this->m_B=b;
		}

		//成员函数实现加号运算符重载
		Person operator+ (const Person &p)
		{
			Person temp;
			temp.m_A=this->m_A+p.m_A;
			temp.m_B=this->m_B+p.m_B;
			return temp;
		}
	public:
		int m_A;
		int m_B;
};

//全局函数实现加号运算符重载
Person operator+ (const Person &p1 ,const Person &p2)
	{
		Person temp(0,0);
		temp.m_A=p1.m_A+p2.m_A;
		temp.m_B=p1.m_B+p2.m_B;
		return temp;
	}

//运算符重载可以发生函数重载
Person operator+ (const Person p2,int val)
{
	Person temp;
	temp.m_A=p2.m_A+val;
	temp.m_B=p2.m_B+val;
	return temp;
}

void test()
{
	Person p1(10,10);
	Person p2(20,20);

	// 成员函数方式
	Person p3=p1+p2;//相当于p2.operator+(p1)
	cout<<"m_A:"<<p3.m_A<<"m_B:"<<p3.m_B<<endl;

	Person p4=p3+10; //相当于operator+(p3,10)
	cout<<...<<endl;
}
int main()
{
	... ...
}

总结1: 对于内置的数据类型的表达式的运算符是不可能改变的
总结2:不能滥用运算符重载(例如operator+函数中写成减法)

2. 左移运算符重载

作用:可以输出自定义数据类型

class Person
{
	friend ostream & operator <<(ostream & out ,Person &p);
	public:
		Person(int a,int b)
		{
			this->m_A = a;
			this->m_B = b;
		}
		//成员函数实现不了, p<<cout 不是我们想要的效果
		//void operator << (Person &p) { }
	private: int m_A;
			 int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream & operator << (ostream &out,Person &p)
{
	out<<"a:"<<p.m_A<<"b:"<<p.m_B;
	return out;
}

void test()
{
	Person p1(10,20);
	cout<<p1<<"hello"<<endl;
}

int main()
{
	... ...
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

3. 递增运算符重载

int a=10;
cout<<++a<<endl; //输出11
cout<<a<<endl; //输出11
//前置递增,先进行递增,再输出表达式

int b=10;
cout<<b++<<endl; //输出10
cout<<b<<endl; //输出11
//后置递增,先进行表达式运算,再让变量进行递增操作
//前置递增与后置递增的区别

int main()
{
	int a=10;
	a++; //等价于a=a+1
	cout<<a<<endl; //输出11

	int b=10;
	++b;
	cout<<b<<endl; //输出11

//前置递增先对变量进行++,再计算表达式
int a2=10;
int b2=++a2*10;
cout<<a2<<endl; //输出11
cout<<b2<<endl; //输出110

//后置递增先计算表达式,后对变量进行++
int a3=10;
int b3=a3++*10; //先乘以10,再+1
cout<<a3<<endl; //输出11
cout<<b3<<endl; //输出100
}
class MyInteger
{
	friend ostream& operator <<(ostream& out,MyInteger myint);

	public:
		MyInteger()
		{
			m_Num=0;
		}
		
	//前置++
	MyInteger & operator++() 
	{
		//先++
		m_Num++;
		//再返回
		return *this;
		
		/*此处为什么是返回引用,能不能是值?不能。返回引用是为了
			一直对一个数据进行递增操作*/
	}
	/*若是内置数据类型
	{
		int a=0;
		cout<<++(++a)<<endl; 输出2
		cout<<a<<endl; 输出2
    }*/

	/*若 MyInteger operator ++() {... ...}
	void test()
	{
		MyInteger myint;
		cout<<++(++myint)<<endl; 输出2
		cout<<myint<<endl; 输出1,原因是myint做了一次++后,
		返回的是一个新的变量
	}*/

	//后置++
	MyInteger operator++(int)
	{
       //int代表占位参数,可以用于区分前置和后置递增

	  //先记录当时结果,后递增,最后将记录结果做返回
	  MyInteger temp=*this; 
	  m_Num++;
	  return temp;
	}
	private:
		int m_Num;
};

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

//前置++,先++,再返回
void test01()
{
	MyInteger myint;
	cout<<++myInt<<endl;
	cout<<myInt<<endl;
}

后置++,先返回,再++
void test02()
{
	MyInteger myInt;
	cout<<myInt++<<endl;
	cout<<myInt<<endl;
}

int main() {... ...}

总结:前置递增返回引用,后置递增返回值

4. 赋值运算符重载

C++编译器至少给一个类添加4个函数
1)默认构造函数(无参,函数体为空)
2)默认析构函数(无参,函数体为空)
3)默认拷贝构造函数,对属性进行值拷贝
4)赋值运算符operator=,对属性进行值拷贝

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

class Person
{
	public:
		Person (int age)
		{
			//将年龄数据开辟到堆区
			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 test01()
{
	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()
{... ...}

5. 关系运算符重载

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

bool operator ==(Person &p)
{
	if(this->m_Nane==p.m_Name && this->m_Age==p.m_Age)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool operator !=(Person &p)
{
	if(this->m_Nane==p.m_Name && this->m_Age==p.m_Age)
	{
		return false;
	}
	else
	{
		return true;
	}
}

6. 函数调用运算符重载

1)函数调用运算符()也可以重载
2)由于重载后使用的方式非常向函数的调用,因此称为仿函数
3)仿函数没有固定写法,非常灵活

class Myprint
{
	public:
		void operator()(string test)
		{
			cout<<test<<endl;
		}
};

void test01()
{
	//重载()操作符也称为仿函数
	Myprint myFunc;
	myFunc("hello"); 
}
class Myadd
{
	public:
		int operator()(int v1,int v2)
		{
			return v1+v2;
		}	
};
void test02()
{
	Myadd add;
	int ret=add(10,10)
	cout<<"ret="<<ret<<endl;

	//匿名函数对象
	cout<<Myadd()(100,100)<<endl;
	//创建了一个匿名对象,特点是当前行执行完了。立即被释放
}

int main() {...  ...}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-05 23:22:06  更:2022-07-05 23:24:54 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 15:33:02-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码