当前位置: 首页 > news >正文

类中的流操作符的重载

一:理解cout的本质

https://legacy.cplusplus.com/reference/iostream/cout/?kw=cout

以上官网中对cout的描述如下:

解释:

我们只用知道以下即可:
①:cout 是类 ostream 的对象

②:作为 ostream 类的对象,可以以cout<<的形式写入数据。

③:该对象在头文件 <iostream> 中声明,具有外部链接和静态存储期:它在程序的整个生命周期内存在。

Q:cout<<会自动识别类型的本质是什么?

A:本质是类ostream中已经给你实现好了对于<<操作符的重载,而且对于不同类型的数据都分别进行了<<操作符的重载

---------------------------------------------------------------------------------------------------------------------------------

对于双操作数的运算符的理解:

对于<<的认识:

需知:系统规定双操作数的运算符 第一个参数是左操作数 第二个参数是右操作数

所以:<<是一个双操作数的操作符 第一个参数为左操作数cout 第二个参数为右操作数变量 

---------------------------------------------------------------------------------------------------------------------------------

二:在类中进行<<操作符的重载

假设在日期类中进行<<操作符的重载

①:参数颠倒的情况

类中的声明:

class Date {
public:
	Date(int year, int month, int day) 
{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print() const {
		printf("%d-%d-%d\n", _year, _month, _day);
	}
 
	void operator<<(ostream& out);
 
private:
	int _year;
	int _month;
	int _day;
};

类外的定义: 

void Date::operator<<(ostream& out) 
{
	out << _year << "/" << _month << "/" << _day << endl;
}

main函数中的使用:

 

发现main函数中无法使用

 

解释: 

①:如需知所言,所以我们平时写的cout<<d1,cout就是<<的第一个参数,d1是<<的第二个参数,但是当我们像上面那样写,第一个参数是隐藏的this!所以和我们想要的相反(此时d1 << cout 是可以的)

②:所以我们要将<<操作符重载声明和定义都写在类外,这样就避免了第一个参数被this抢占了,也就是全局函数

②:无法访问私有成员变量的情况

类外的声明:

void operator<<(ostream& out, const Date&d);
//而第一个参数不加const是因为 其会改变 记住就行

类外的声明: 

void operator<<(ostream& out, const Date& d) 
{
out << d._year << "/" << d._month << "/" << d._day << endl;
}

解释:此时在声明中就会报错,因为 因为全局函数没有权限访问私有成员变量,所以我们还得加上友元

③:无法连续使用<<的情况

如下:

class Date {
public:
	friend void operator<<(ostream& out, const Date& d);  // 友元的声明
	//...
 
private:
	int _year;
	int _month;
	int _day;
};

 

新的问题:

无法 cout << d1 << d2,因为真正的cout可以连续的打印

连续插入 d1 和 d2 实际上就是两次函数的调用,这里先执行的是 cout << d1,

因为调用函数后返回值是 void,void 会做这里的左操作数,

所以当然不支持连续输出了,

解决方案:

我们把返回值改为 ostream 就行,把 out这个对象返回回去。

④:正确的写法

进行如下的改进:

类中的声明: 

class Date {
public:
    // ...
	friend ostream& operator<<(ostream& out, const Date& d);    // 友元的声明
 
private:
	int _year;
	int _month;
	int _day;
};

类外的定义: 

#include "Date.h"
 
ostream& operator<<(ostream& out, const Date& d) {
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}

 

int main(void)
{
	Date d1(2022, 3, 20);
	Date d2(2021, 5, 1);
	cout << d1 << d2;
 
	return 0;
}

此时在main函数可以连续的打印了 

 

三:在类中进行>>操作符的重载

类中的声明:

class Date {
public:
    // ...
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
 
private:
	int _year;
	int _month;
	int _day;
};

 类外的的定义:

#include "Date.h"
 
istream& operator>>(istream& in, Date& d) {
	in >> d._year >> d._month >> d._day;
	return in;
}

 main函数中的使用:

int main(void)
{
	Date d1(2022, 3, 20);
	Date d2(2021, 5, 1);
	
	printf("请输入两个日期:\n");
	cin >> d1 >> d2;
 
	printf("你输入的日期是:\n");
	cout << d1 << d2;
 
	return 0;
}

解释:和cout类似,cin是istream类的全局对象

 

相关文章:

  • Python 数据可视化(一)熟悉Matplotlib
  • python判断、循环、range语句
  • Servlet简介
  • 2025最新Nginx高频面试题
  • Python的pdf2image库将PDF文件转换为PNG图片
  • windous 下 ollama 迁移到 D 盘
  • 【2025年2月28日稳定版】小米路由器4C刷机Immortalwrt 23.05.4系统搭载mentohust 0.3.1插件全记录
  • 【Mark】记录用宝塔+Nginx+worldpress+域名遇到的跨域,301,127.0.0.1,CSS加载失败问题
  • 如何流畅访问github
  • 架构案例:从初创互联网公司到分布式存储与反应式编程框架的架构设计
  • 2011-2019年各省移动电话普及率数据
  • Deepseek对ChatGPT的冲击?
  • (平衡二叉树 判断是否为AVL树 )leetcode110
  • Python 模块与包:从零到自定义的全面指南
  • 第五章 activiti流程 “权限与部署指挥部”
  • Zotero外接通义千问 API进行翻译
  • 跟着源码实现LevelDB(二)util/status.cc
  • 独立开发增长黑客教程
  • 让deepseek更专业的提示词教程
  • CSS Selectors
  • 网站建设打造学院/百度推广app怎么收费
  • 建设部网站举报/关键词优化排名
  • 北京网站建设 专业10年/宁波seo
  • 做网页推广的网站/西安分类信息seo公司
  • 做众筹网站要什么资质/seo推广优化方案
  • 广州建设工程中心网站/公司网站制作