C++学习(十二)—运算符重载(二)

智能指针

用途:用来托管堆区创建的对象的释放

#include<iostream>

using namespace std;

class Person
{
public:
	Person(int age)
	{
		this->m_age = age;
		cout << "Person的有参函数构造" << endl;
	}
	void showage()
	{
		cout << "年龄为:" << this->m_age << endl;
	}

	~Person()
	{
		cout << "Person的析构函数调用" << endl;
	}
	int m_age;
};


//  智能指针  用来托管new出来的对象的释放

class SmartPointer
{
public:
	SmartPointer(Person *p)
	{
		cout << "SmartPointer有参构造函数调用" << endl;
		this->person = p;
	}

	//  重载指针运算符
	Person * operator->()
	{
		return this->person;
	}
	Person & operator*()
	{
		return *(this->person);
	}


	Person * person;
	~SmartPointer()
	{
		if (this->person != NULL)
		{
			cout << "SmartPointer析构函数调用" << endl;
			delete this->person;
			this->person = NULL;
		}
	}
};

void test01()
{
	//	Person *p = new Person(10);

	//	delete p;
	SmartPointer sp = SmartPointer(new Person(18));
	sp->showage();  //  sp->->showage() 编译器简化为  sp->showage;

	(*sp).showage();
}



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

赋值运算符重载

系统会默认给一个类添加4个函数:默认构造、拷贝构造、析构、operator=
由于系统提供的operator= 会进行简单的值拷贝,导致如果属性中有堆区的数据,就会进行重复释放 解决方案 需要重载 operator=

#include<iostream>
#include<string>
using namespace std;

class Person
{
public:
	Person(){}
	Person(const char *name, int age)
	{
		this->m_name = new char[strlen(name) + 1];
		strcpy_s(this->m_name, strlen(name) + 1, name);
		this->m_age = age;
	}

	//  系统会默认给一个类  创建至少3个函数  默认构造  有参构造  拷贝构造(简单值拷贝)  operator=(值拷贝)  
	Person(const Person &p)
	{
		this->m_name = new char[strlen(p.m_name) + 1];
		strcpy_s(this->m_name, strlen(p.m_name) + 1, p.m_name);
		this->m_age = p.m_age;
	}

	Person & operator=(const Person &p) 
	{
		if (this->m_name != NULL)
		{
			delete[]this->m_name;
			this->m_name = NULL;
		}

		this->m_name = new char[strlen(p.m_name) + 1];
		strcpy_s(this->m_name, strlen(p.m_name) + 1, p.m_name);
		this->m_age = p.m_age;
		return *this;
	}

	~Person()
	{
		//	cout << "析构" << endl;
		if (this->m_name != NULL)
		{
			delete[] this->m_name;
			this->m_name = NULL;
		}
	}

public:
	char *m_name;
	int m_age;
};


void test01()
{
	Person p1("Tom",18);

	Person p2("Jerry", 19);

	p1 = p2;

	cout << "p1的姓名:" << p1.m_name << " p1的年龄:" << p1.m_age << endl;

	cout << "p2的姓名:" << p2.m_name << " p2的年龄:" << p2.m_age << endl;
}

void test02()
{
	/*int a = 10;
	int b = 20;
	int c ;

	c = a = b ;

	cout << "a = " << a << " b = " << b << " c = " << c << endl;*/

	Person p1("Tom", 18);

	Person p2("Jerry", 19);

	Person p3;

	p3 = p1 = p2;

	cout << "p1的姓名:" << p1.m_name << " p1的年龄:" << p1.m_age << endl;

	cout << "p2的姓名:" << p2.m_name << " p2的年龄:" << p2.m_age << endl;

	cout << "p3的姓名:" << p3.m_name << " p3的年龄:" << p3.m_age << endl;

	Person p4(p3);

	cout << "p4的姓名:" << p4.m_name << " p4的年龄:" << p4.m_age << endl;

}



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

关系运算符重载

#include<iostream>
#include<string>
using namespace std;

class Person
{
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}

	bool operator==(const Person &p)
	{
		/*if (this->m_name == p.m_name&&this->m_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}*/
		return this->m_name == p.m_name&&this->m_age == p.m_age;
	}

	bool operator!=(const Person &p)
	{
		return !(this->m_name == p.m_name&&this->m_age == p.m_age);
	}

	string m_name;
	int m_age;
};



void  test01()
{
	int a = 10;
	int b = 20;
	if (a == b)
	{
		cout << "a等于b" << endl;
	}
	else
	{
		cout << "a不等于b" << endl;
	}
}

void test02()
{
	Person p1("Tom",18);

	Person p2("Tom",18);

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



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

函数调用运算符重载

#include<iostream>
#include<string>
using namespace std;

class MyFunc
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}
};

class MyAdd
{
public:
	int operator()(int a, int b)
	{
		return a + b;
	}
protected:
private:
};


void text01()
{
	MyFunc myfunc;
	myfunc("hello,world!");
}

void text02()
{
	cout << MyAdd()(10, 10) << endl;
}

int main()
{
	//	text01();
	text02();
	system("pause");
	return 0;
}

逻辑运算符

不要重载与和或

符号重载总结

  • = [ ] ()和->操作符只能通过成员函数进行重载

  • << 和>>只能同过全局函数配合友元函数进行重载

  • 不要重载 && 和 || 操作符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

walkerrev_ll

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

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

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

打赏作者

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

抵扣说明:

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

余额充值