【C++】类与对象(赋值运算符重载)

[本节内容]

赋值运算符重载
类的六个默认成员函数在下面博客中讲到:
类与对象:https://blog.csdn.net/ly_6699/article/details/87870429

5.1 运算符重载

C++为了增加代码的可读性引入运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与普通的函数类似。
函数名字为:关键字operator 后面接需要重载的运算符符号
函数原型:返回值类型operator操作符(参数列表)
注意:
1)不能通过连接其他符号来创建新的操作符:比如 operator@
2) 重载操作符必须有一个类类型或者联合类型的操作数
3)用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
4)作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
5).*、::、sizeof、?:、. 注意以上五个运算符不能重载!!!

//重载全局 operator==
//1.公有成员变量,类外实现重载
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day; 
	}
//private:
 //private 限定类外无法访问。 重载全局运算符就要求它们为公有成员所以暂时屏蔽
	int _year;
	int _month;
	int _day;
};
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year&& d1._month == d2._month&&d1._day == d2._day;
}
void Test()
{
	Date d1(2001, 1, 1);
	Date d2(2010, 2, 3);
	cout << (d1 == d2) << endl;    //或 operator==(d1,d2)
}
int main()
{
	Test();
	system("pause");
	return 0;
}

公有的成员变量方便了重载运算符,但是问题来了,封装性如何保证呢?
这里我们可以用友元(后面学到)或者干脆重载成成员函数来解决。

//2.私有成员变量,类内实现重载
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==( const Date &d2)   //第一个操作数默认为this指向的调用函数的对象
	{
		return _year == d2._year&&_month == d2._month&&_day == d2._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
void Test()
{
	Date d1(2001, 1, 1);
	Date d2(2010, 2, 3);
	cout << (d1 == d2) << endl;    //或d1.operator==(d2)
}
int main()
{
	Test();
	system("pause");
	return 0;
}
5.2赋值运算符重载
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date operator=(const Date& d)    //传引用
	{
		if (this != &d)      //this是地址
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;     //为实现连续赋值
	}
private:
	int _year;
	int _month;
	int _day;
};

赋值运算符主要有以下几点:
1.参数类型
2.返回值
3.检测是否是给自己赋值
4.返回*this(指针)
5.一个类如果没有显式定义赋值运算符重载,编译器会生成默认函数,完成对象按字节序的值拷贝

// 默认赋值重载运算符
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{

	Date d1;
	Date d2(2019, 1, 2);
	d1 = d2;   //这里d1会调用编译器生成的operator=完成赋值
	system("pause");
	return 0;
}

那么既然编译器自己生成的赋值重载函数就已经可以完成字节序的值拷贝了,我们还需要自己实现吗?当然向日期类这样的类是没有必要的 ,那么下面的类呢?

// String 类
class String
{
public:
	String(const char* str = "jack")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};
int main()
{
	String s1("hello");
	String s2("world");
	s1 = s2;       //程序在这里会崩溃,因为只有一个空间但要调用两次~String()
	//这就是浅拷贝容易引发的问题,这时只能用深拷贝解决
}

**解决方法:**先为新对象开辟一个空间,再进行值拷贝

class String
{
public:
	String(const char* str = "jack")
	{
		_str = (char*)malloc(strlen(str) + 1);
		
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
	String& operator=(const String& s)   //重载赋值运算符
{
	if (this != &s)
	{
		char* pstr = (char*)malloc(strlen(s._str) + 1);  //再开辟一段空间
		strcpy(pstr,s._str);
		delete[]_str;           //手动释放刚开辟的空间
		_str = pstr;
	}
	return *this;
}
private:
	char* _str;
};
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值