运算符重载

运输算符重载

运算符重载时具有特殊函数名的函数上述,也有返回值类型,函数名,参数列表

运输算符重载特点

  • 不能创建新操作符,eg:operator@
  • 重载操作符必须有一个类类型参数
  • . sizeof ?:: ::不能重载
  • 作为类成员函数,第一个参数时隐藏的this
cpp 复制代码
class Date
{
public:
	Date(int year=1,int month=1,int day=1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
//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;
}


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

	bool operator== ( const Date& d2)
	{
		return _year == d2._year && _month == d2._month && _day == d2._day;
	}

	private:
	int _year;
	int _month;
	int _day;
};




int main()
{
	Date d1;
	Date d2;
	std::cout << (d1 == d2);
	return 0;
}

赋值运算符重载

  • const T& 传引用返回可以调高效率,也可以连续赋值
  • 传引用参数,也可以调高效率
  • 检测是不是自己给自己赋值
  • 返回*this,要符合连续赋值的含义
cpp 复制代码
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	bool operator== ( const Date& d2)   
	{
		return _year == d2._year && _month == d2._month && _day == d2._day;
	}

	Date& operator=(const Date& d2)
	{
		_year = d2._year;
		_month = d2._month;
		_day = d2._day;
		return *this;
	}

	void Print()
	{
		std::cout << " " << _year << std::endl;
		std::cout << " " << _month << std::endl;
		std::cout << " " << _day << std::endl;
	}

	private:
	int _year;
	int _month;
	int _day;
};




int main()
{
	Date d1;
	Date d2(2025, 12, 29);
	Date d3(2026, 12, 29);
	d1 = d2 = d3;
	d1.Print();
	return 0;
}

赋值运算符不能重载成全局函数,必须定义为成员函数

原因:赋值运算符如果不显示实现,编译器会自动生成,此时用户自己写的赋值运算符系统默认生成的冲突了

默认生成的赋值运算符按字节拷贝。内置成员是自动赋值的,自定义类型成员变量需要调用相应的运算赋值符重载完成赋值

cpp 复制代码
class Time
{
public:
	Time(int s = 0)
	{
		_s = s;
	}
	Time& operator=(const Time& t2)
	{    
        if(this!=t2)
        {
            _s = t2._s;
		    return *this;
        }
		
	}
	void Print()
	{
		std::cout << " " << _s << std::endl;
	}
private:
	int _s;
};

class Date
{
public:

	Date(int year=0, int month=0, int day=0,const Time& time=0)
	{
		_year = year;
		_month = month;
		_day = day;
		_time = time;
	}

	bool operator== ( const Date& d2)   
	{
		return _year == d2._year && _month == d2._month && _day == d2._day;
	}

	//Date& operator=(const Date& d2)
	//{
	//	_year = d2._year;
	//	_month = d2._month;
	//	_day = d2._day;
	//	_time = d2._time;
	//	return *this;
	//}

	void Print()
	{
		std::cout << " " << _year << std::endl;
		std::cout << " " << _month << std::endl;
		std::cout << " " << _day << std::endl;
		_time.Print();
	}

	private:
	int _year;
	int _month;
	int _day;//内置成员变量可以不写,但是自定义成员变量要写
	Time _time;
};




int main()
{
	Time t(60);
	Date d1;
	Date d2(2025, 12, 29,t);
	d1 = d2;//自动调用了Time的operator=
	d1.Print();
	return 0;
}

赋值运算符也会生成默认的函数,完成字节拷贝,但是一旦涉及的堆空间申请一档要手动写operator=,和析构函数有点像,都是涉及到了深拷贝

前置++和后置++

为了区分前置和后置++,C+规定:后置++多增加一个int类型的参数,但调用函数时该参数不传递,编译器自动传递

const成员

const修饰的成员函数实际上是修饰隐含的this指针,表明该成员函数不能对类的任何成员进行修改

  • const对象只能调用const成员函数
  • const成员函数可以调用非const成员函数
  • const成员函数可以访问成员变量(实质上是const成员变量)
相关推荐
ziguo11223 小时前
深入浅出 C/C++ 数据类型:从入门到踩坑
linux·c语言·c++·windows·visual studio
白色冰激凌3 小时前
[SECS/GEM研究] (三)SECS-I 串口上消息怎么分块和重传
c++·secs/gem
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(中)
android·c++·go
光头闪亮亮4 小时前
Fyne ( go跨平台GUI )项目实战-项目开发必备基础知识(下)
android·c++·go
_wyt0014 小时前
洛谷 P7912 [CSP-J 2021] 小熊的果篮 题解
c++·队列
choumin5 小时前
创建型模式——原型模式
c++·设计模式·原型模式·创建型模式
code_pgf6 小时前
C/C++ 常用容器功能汇总
c语言·开发语言·c++
王维同学7 小时前
Credential Provider、Filter 与 PLAP 的 CLSID 枚举
c++·windows·安全·注册表
小保CPP7 小时前
OpenCV C++基于模糊数学的图像修复
c++·人工智能·opencv·计算机视觉
Carlgood-Minecraft8 小时前
随机分位系统破解版 (BPS)Version-B4.2
c++