主页 > 人工智能  > 

类中的流操作符的重载

类中的流操作符的重载
一:理解cout的本质

legacy.cplusplus /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类的全局对象

 

标签:

类中的流操作符的重载由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“类中的流操作符的重载