CD21.【C++ Dev】类和对象(12) 流插入运算符的重载

目录

1.要使用的代码

2.知识回顾

3.<<流插入运算符重载

版本1

[为什么等价为d1 << cout?](#为什么等价为d1 << cout?)

版本2

方法1:修改头文件

方法2:不写在成员函数中,而是在类外写

版本3

版本4:私有情况下的访问(使用公有成员函数)

分析问题

版本5:私有情况下的访问(使用友元函数)(★推荐★)

其他提醒


1.要使用的代码

使用日期类对象:

Date.h:

cpp 复制代码
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
	Date(int year = 0, int month = 0, int day = 0);
	Date(const Date& x);
	void Print();

	int _year;
	int _month;
	int _day;
};

Date.cpp:

cpp 复制代码
#include "Date.h"
Date::Date(int year , int month, int day)
{
	//cout <<"构造函数:Date::Date(int year , int month, int day)" << endl;
	_year = year;
	_month = month;
	_day = day;
}

Date::Date(const Date& x)
{
	// << "拷贝构造函数:Date::Date(const Date& x)" << endl;
	_year = x._year;
	_month = x._month;
	_day = x._day;
}

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

main.cpp:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
	d1.Print();
	return 0;
}

运行结果:

2.知识回顾

>>和<<

CC2.【C++ Cont】初认识cout,cin和endl

3.<<流插入运算符重载

如果main.cpp中不使用d1.Print(),而想使用cout<<来打印年月日,则需要对<<运算符重载,让它可以支持自定义类型的打印(不能使用printf,只支持内置类型,)

库里面只实现了这些内置类型:

显然是operator<<函数重载了

参考资料 点我跳转

发现:cout为ostream的类对象


cpp 复制代码
cout << d1;

观察上面这个式子,<<有两个操作数,一个是cout,一个是d1

版本1

可以手动控制格式,如_year._month._day

cpp 复制代码
void Date::operator<<(ostream& out)//其实不能返回void,这个会在版本3中修正
{
	out << _year << "." << _month << "." << _day;
}

测试代码:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
    //本质上调用的式子,调用d1的operator<<成员函数
    //第一个参数是左操作数,第二个参数是右操作数
	d1.operator<<(cout);
	return 0;
}

运行结果:看起来没有问题

按照之前讲过的文章,d1.operator<<(cout)等价于d1 << cout,但这种写法很奇怪,与平常习惯上写的cout << d1反过来

为什么等价为d1 << cout?

观察函数头:

cpp 复制代码
void Date::operator<<(ostream& out)

其实隐藏了第一个参数this,实际上为

cpp 复制代码
void Date::operator<<(const Date* this, ostream& out)

this指针强制占用了左操作数,导致d1 << cout中d1必须放在左侧

版本2

由上述分析可知:要改cout<<d1,有两个方法:

方法1:修改头文件

此方法不推荐,头文件不建议修改

方法2:不写在成员函数中,而是在类外写

如果在类里面写,this会隐式占用第一个参数,则可以放在类外写

Date.h里添加声明,Date.cpp里写定义

测试代码:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
	cout << d1;
	return 0;
}

运行结果:貌似没有问题

但如果连续使用<<就会出现错误,例如cout << d1 << d1;

下面编译报错:

分析原因:

cout从左向右 执行,则cout << d1 << d1转换为void << d1丢失了cout,导致无法写入ostream,观察到cplusplus网上的operator<<的声明,正确的返回值应该是ostream&

版本3

cpp 复制代码
ostream& operator<<(ostream& out, const Date& d)//从返回值类型来看,可以支持连续流插入
{
	out << d._year << "." << d._month << "." << d._day;
	return out;
}

例如:cout<<d2<<d3<<d1→cout<<d3<<d1→cout<<d1

测试代码:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
	cout << d1 << d1;
	return 0;
}

若理解了两个参数的含义和用法,则cout << d1 << d1;的等价写法为

operator<<(operator<<(cout, d1), d1);

运行结果:

版本4:私有情况下的访问(使用公有成员函数)

如果成员变量_year、_month和_day改成私有,则在类外定义的operator<<是不能访问的

创建一个成员函数取得_year、_month和_day的值然后传参给operator<<

Date.cpp:

cpp 复制代码
ostream& operator<<(ostream& out, const Date& d)
{
	out << d.GetYear() << "." << d.GetMonth() << "." << d.GetDay();
	return out;
}

Date.h:

cpp 复制代码
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
    //省略其他成员函数
	int GetYear()
	{
		return _year;
	}

	int GetMonth()
	{
		return _month;
	}

	int GetDay()
	{
		return _day;
	}

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

main.cpp:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
	cout << d1;
	return 0;
}

但这样写会报错:

发现报错报的都是"不能将"this"指针从"const Date"转换为"Date &""

分析问题

看一下传参过程:

(权限不能放大)

问题:隐含的this参数的默认类型为Date*,而d.GetYear()、d.GetMonth()和d.GetDay()传的类型是const Data&,要确保this指向的参数不能被修改,只需要在成员函数的后面加上const来限制this

cpp 复制代码
int GetYear() const
{
	return _year;
}

int GetMonth() const
{
	return _month;
}

int GetDay() const
{
	return _day;
}

重新测试代码,运行结果:正常打印:

版本5:私有情况下的访问(使用友元函数)(★推荐★)

友元函数

除了版本4之外,还要另外一种写法:使用友元函数,其特点为:不受访问限定符限制,突破封装的限制

(这里先简单了解用法,后面的文章会详细讲解)

Date.cpp的operator<<改成

cpp 复制代码
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "." << d._month << "." <<d. _day;
	return out;
}

Date.h中在class Date里面的任意位置(因为只是一个声明)添加友元函数声明

例如:

cpp 复制代码
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
public:
    //省略成员函数
private:
	int _year;
	int _month;
	int _day;
};

或者这样写:

cpp 复制代码
class Date
{
public:
    friend ostream& operator<<(ostream& out, const Date& d);
    //省略成员函数
private:
	int _year;
	int _month;
	int _day;
};

或者这样写:

cpp 复制代码
class Date
{
public:
    //省略成员函数
private:
    friend ostream& operator<<(ostream& out, const Date& d);
	int _year;
	int _month;
	int _day;
};

测试代码:

cpp 复制代码
#include "Date.h"
int main()
{
	Date d1(2025, 3, 28);
	cout << d1;
	return 0;
}

运行结果:

其他提醒

operator<<(ostream& out, const Date& d)的第一个参数不能用const修饰,因为向流中插入东西,需要改变内容,不可用const修饰

流提取运算符参见下篇

相关推荐
第九序言T10 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
Sakura_j_h_11 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
ZhengEnCi11 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
龙亘川11 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
程序喵大人11 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型
秋天的怀念_David12 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Raas10012 小时前
AI网关部署方案?从部署到上线全流程
java·开发语言·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品
Pniubi12 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_61812 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang12 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain