C++进阶11(运算符重载(赋值、关系运算符、函数调用运算符))

赋值运算符重载

#include <iostream>

using namespace std;

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

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

//赋值运算符重载
class Person
{
public:
	Person(int age)
	{
		m_Age = new int(age);//把年龄的数据创建在堆区。堆区的数据由程序员手动开辟和释放
	}
	~Person()//释放堆区的内存时 p1赋值给p2会导致内存重复释放
	{
		if (m_Age != NULL) 
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	//重载赋值运算符
	Person& operator=(Person& p)//返回值类型是void时无法实现连等
	{
		//编译器提供的事浅拷贝
		//m_Age = p.m_Age;

		//应该先判断是否有属性在堆区,如果有限释放干净然后在深拷贝
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;

		}
		m_Age = new int(*p.m_Age);//深拷贝操作

		//返回对象本身
		return *this;//实现连等操作
	}

	int *m_Age;//将年龄写成指针
};


void test01()
{
	Person p1(18);

	Person p2(20);

	Person p3(30);

	p2 = p1;//赋值操作
	p3 = p2 = p1;  //连等时必须重载时返回值是数据自身(以引用形式返回)
	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
}
int main()
{
	test01();
	/*int a = 10;
	int b = 20;
	int c = 30;
	c = b = a;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	cout << "c=" << c << endl;*/
	cout << "\n " << endl;
	system("pause");
	return 0;
}

关系运算符重载

#include <iostream>

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

class Person
{
public :
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	//重载 == 号
	bool operator==(Person& p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}
	bool operator!=(Person& p)
	{
		if (this->m_name != p.m_name || this->m_age != p.m_age)
		{
			return true;
		}
		return false;
	}
	/*bool operator!=(Person& p)
	{
		if (this->m_name == p.m_name || this->m_age == p.m_age)
		{
			return false;
		}
		return true;
	}*///同样可以实现不等号的重载效果

	string m_name;
	int m_age;
};

void test01()
{
	Person p1("Tom", 18);
	Person p2("Tom", 18);
	if (p1 == p2)
	{
		cout << "p1和p2是相等的" << endl;
	}
	else
	{
		cout << "p1和p2是不相等的" << endl;
	}
	if (p1 != p2)
	{
		cout << "p1和p2是不相等的" << endl;
	}
	else
	{
		cout << "p1和p2是相等的" << endl;
	}

}

int main()
{
	test01();
	cout << "\n " << endl;
	system("pause");
	return 0;
}

函数调用运算符重载

#include <iostream>

using namespace std;

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

//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符

	void operator()(string test)
	{
		cout << test << endl;
	}
};


void MyPrint02(string test)
{
	cout << test << endl;
}

void test01()
{
	MyPrint myPrint;

	myPrint("hello world");

	MyPrint02("hello world2");
}

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

class MyAdd
{
public:
	int operator()(int num1 ,int num2)
	{
		return num1 + num2;
	}
};

void test02()
{
	MyAdd myadd;
	int result = myadd(100, 100);
	cout << "result= " << result << endl;

	//匿名函数对象  语法:类名()
	cout << MyAdd()(100, 100) << endl;//MyAdd()为匿名对象,特点:当前行执行完之后被释放
}
int main()
{
	test01();
	cout << "\n " << endl;
	test02();
	cout << "\n " << endl;
	system("pause");
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值