类和对象(2)——赋值运算符重载

目录

1.1 运算符重载

1.2 赋值运算符重载

1.3 前置++和后置++重载


1.1 运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号

函数原型:返回值类型 operator操作符(参数列表)
注意:

       (1)不能通过连接其他符号来创建新的操作符:比如operator@

        (2)重载操作符必须有一个类类型参数

       (3)用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义

        (4)作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this

        (5).*    ::    sizeof    ?:    .    注意以上5个运算符不能重载

例:判断两个对象是否相等,就需要重载==运算符

class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==(const Date& d)
	{
		return _year == d._year&&
				_month==d._month&&
				_day==d._day;	
	}

private:
	int _year;
	int _month;
	int _day;	
};
 int main()
 {
	 Date d1(2022,11, 20);
	 Date d2(d1);
	 d2 ==d1;
 }

1.2 赋值运算符重载

        1.赋值运算符重载格式:

                参数类型:const T& ,传递引用可以提高参数效率

                返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值

                检测是否自己给自己赋值

                返回*this :要复合连续赋值的含义 

class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	bool operator==(const Date& d)
	{
		return _year == d._year&&
			_month == d._month&&
			_day == d._day;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}	
		return *this;
	}

private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2022, 11, 20);
	Date d2;
	Date d3;
	d3 = d2 = d1;
}

         2.赋值运算符只能重载成类的成员函数不能重载成全局函数

class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	int _year;
	int _month;
	int _day;
};
Date& operator=( Date& left,const Date& right)  //编译失败
{
	if (&left != &right)
	{
		left._year = right._year;
		left._month = right._month;
		left._day = right._day;
	}
	return left;
}

原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。

        3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。

class Time
{
public:
	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
	}
	Time& operator=(const Time& t)
	{
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}
		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	Date d2;
	d1 = d2;
	return 0;
}

 可以看出,Date类没有显式定义赋值运算符重载,Date类在编译时,编译器会生成一个默认的赋值运算符重载给内置类型直接赋值,而给自定义类型赋值时,必须调用相关类的赋值运算符重载去完成。

        4.如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必须要实现。

例如:栈(stack)类,用一个s1对象给另一个s2对象赋值,若没有显示定义赋值运算符重载,则编译器默认生成,采用浅拷贝的方式来赋值,则会使s1和s2共享同一份内存空间。在销毁时,空间被销毁两次,会发生错误。

1.3 前置++和后置++重载

class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//前置++重载,返回+1后的结果
	Date& operator++()
	{
		_day += 1;
		return *this;
	}
	//后置++重载,返回+1前的结果,为让后置++和前置++形成重载,给后置++多一个int类型的参数
	Date operator++(int)
	{
		Date temp(*this);
		_day += 1;
		return temp;
	}
	
	
private:
	int _year;
	int _month;
	int _day;	
};
int main()
{
	Date d1(2022, 11, 20);
	Date d2, d3;
	d2 = ++d1;
	d3 = d1++;
	return 0;
}

解释:d1先++再给d2赋值,d1变为2022/11/21,d2则被赋值为2022/11/21;d1先给d3赋值,后++,d3为2022/11/21,d1为2022/11/22。

前置++:

        返回+1之后的结果

        注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率

后置++:

        先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,然后给this+1。

        注意:temp保存this旧值,为临时对象,不能返回引用,只能以值的方式返回。

        前置++和后置++都是一元运算符,运算符重载隐含this指针,故不用传参,为了让前置++与后置++形成正确重载;C++规定,后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递。

        


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值