C++运算符重载详解(13)

目录

运算符重载

一、加号运算符重载

1.1 成员函数重载加号运算符

1.2通过全局函数重载加号运算符

1.3 运算符重载的函数重载

二、 左移运算符重载

2.1成员函数重载左移运算符

2.2 全局函数实现左移运算符

三、递增运算符重载

四、赋值运算符重载

五、关系运算符重载

六、函数调用运算符重载


运算符重载

概述: 在同一作用域中的某个函数运算符指定多个定义,分别称为函数重载运算符重载

当您调用一个重载函数重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策

运算符重载可以实现两个自定义数据类型的相加运算 如 两个person类相加

可重载运算符/不可重载运算符

下面是可重载的运算符列表:

双目算术运算符+ (加),-(减),*(乘),/(除),% (取模)
关系运算符==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符+ (正),-(负),*(指针),&(取地址)
自增自减运算符++(自增),--(自减)
位运算符| (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符=, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放new, delete, new[ ] , delete[]
其他运算符()(函数调用),->(成员访问),,(逗号),[](下标)

下面是不可重载的运算符列表:

  • .:成员访问运算符

  • .*, ->*:成员指针访问运算符

  • :::域运算符

  • sizeof:长度运算符

  • ?::条件运算符

  • #: 预处理符号

一、加号运算符重载

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

语法: operator+

1.1 成员函数重载加号运算符

要实现两个自定义类型相加在没有重载的情况下可以自己写成员函数实现

例如

Person PersonAddPerson(Person &p)   // 类中的成员函数
{
	Person temp;				  // 创建临时变量
	temp.m_a = this->m_a + p.m_a;   // 传进来的p.m_a 与 本身的相加
	temp.m_b = this->m_b + p.m_b;   
	return temp;				   // 返回对象
}


/*
调用时
person p3 = p1.PersonAddPerson(p2);
*/

C++在语法上进行了统一,在用运算符重载的时候使用 operator+ 替换掉 PersonAddPerson,并且在使用时得到了优化

实例

#include<iostream>
using namespace std;
 
class person
{
public:
	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;
};
 
int main()
{
	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_B;
	system("pause");
	return 0;
}

1.2通过全局函数重载加号运算符

实例

#include<iostream>
using namespace std;
 
class person
{
public:
	int m_A;
	int m_B;
};
 
// 全局函数加号运算符重载
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;
}
 
int main()
{
	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_B;
	system("pause");
	return 0;
}

1.3 运算符重载的函数重载

运算符重载了可以实现两个相同类型数的相加,但不能实现对其它类型数的相加如对int型,但是通过运算符函数重载就可以实现。

实例

//运算符重载 可以发生函数重载 
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;
}
// person p3 = p1 + 2;

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

二、 左移运算符重载

作用:可以直接输出对象就把对象的数据输出了即 cout << p <<endl;

2.1成员函数重载左移运算符

一般不会利用成员函数实现左移运算符,因为无法实现 cout 在左侧

2.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& cout, Person& p)
{
	cout << "a:" << p.m_A << " b:" << p.m_B;
	return cout;  // 返回cout实现链式 
}

void test()
{
	Person p1(10, 20);
	cout << p1 << "hello world" << endl; //链式编程
}

int main() 
{
	test();
	system("pause");
	return 0;
}

注意:

  1. cout 属于输出流对象(ostream)

  2. cout 在传递的时候要用引用 因为全局只有一个cout

  3. 某些成员为私有成员时,不能用cout输出对象,解决方法把全局函数声明为类的友元

三、递增运算符重载

作用:使得自定义运算符可以前置加加或者后置加加

class MyInteger 
{
    // 实现左移运算符的时候要声明为友元这样可访问私有成员
	friend ostream& operator<<(ostream& out, MyInteger myint);
public:
	MyInteger() 
    {
		m_Num = 0;
	}
	// 前置++ 返回的是本身
	MyInteger& operator++() 
    {
		// 先++
		m_Num++;
		// 再返回自身的值
		return *this;
	}
	// 后置++  占位参数 用来区分前置后置  只能用int
	MyInteger operator++(int)
    {
		// 先记录当前结果
		MyInteger temp = *this; // 记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
		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;  // 这里myInt++返回的是 记录的值
	cout << myInt << endl;    // 这里myInt 是上面运行后加一的结果
}

int main() 
{
	test01();
	// test02();

	system("pause");
	return 0;
}

注意:前置加加返回的是本身,因为还需要继续使用,后置加加返回的是前面的值


四、赋值运算符重载

作用: 编译器提供的赋值运算符是浅拷贝,如果不重载赋值运算符,那么在两个对象进行赋值操作时,会出现浅拷贝现象导致两个对象指向的是用一个指针地址。当程序执行析构函数的时候就会导致程序崩溃。

class Person
{
public:

	Person(int age)
	{
		// 将年龄数据开辟到堆区
		m_Age = new int(age);
	}

	// 重载赋值运算符 
	Person& operator=(Person &p)
	{
        // 先判断是否有空间存储m_Age 有先释放
		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 p1(18);

	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() 
{
	test01();

	system("pause");
	return 0;
}

注意: 重载返回的是自身值的引用,


五、关系运算符重载

作用: 对自定义类型数据对比

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	};
   
    // 比较的结果只有真假 所以返回bool 型
	bool operator==(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	// 比较的结果只有真假 所以返回bool 型
	bool operator!=(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	string m_Name;
	int m_Age;
};

void test01()
{
	//int a = 0;
	//int b = 0;

	Person a("孙悟空", 18);
	Person b("孙悟空", 18);

	if (a == b)
	{
		cout << "a和b相等" << endl;
	}
	else
	{
		cout << "a和b不相等" << endl;
	}

	if (a != b)
	{
		cout << "a和b不相等" << endl;
	}
	else
	{
		cout << "a和b相等" << endl;
	}
}

int main()
{
	test01();
	system("pause");
	return 0;
}

六、函数调用运算符重载

作用: 通过重载函数调用运算符,可以使得一个类的对象像函数一样被调用。实际上,重载后的函数调用运算符可以用来模拟函数的行为。

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};
void test01()
{
	//重载的()操作符 也称为仿函数
	MyPrint myFunc;
	myFunc("hello world");
}

class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd add;
	int ret = add(10, 10); // 调用add对象,实际上是调用operator()(10, 10)
	cout << "ret = " << ret << endl;

	//匿名对象调用  这里其实创建了一个对象但在使用完后就释放了。
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}

总结:

1:对于内置的数据类型的表达式的的运算符是不可能改变的

2:不要滥用运算符重载


上一篇:C++ 继承详解

下一篇:C++ 多态详解

  • 40
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值