简体   繁体   English

C ++ iostream运算符覆盖了函数的返回类型

[英]C++ iostream operator overladed function return type

I'm still in learning phase of basic formats and commands of C++. 我仍处于C ++基本格式和命令的学习阶段。 I'm now at class operator function overloading and came to << and >> . 我现在在类运算符函数重载,并来到<<>> My question is: when they are defined in friend functions such as below: 我的问题是:在如下所示的朋友函数中定义它们时:

ostream &operator << ( ostream &output, const PhoneNumber &number )

and are called with PhoneNumber class phone like this: 并通过PhoneNumber类phone进行调用,如下所示:

cout << phone << endl;

Why is the friend function returning ostream& ? 为什么好友函数返回ostream& I mean when a function returns a value of a particular type, it is generally received by a fundamental type variable such as bool, int, char, string, and etc. However, for ostream and istream , the returned type of ostream& is not being saved. 我的意思是,当函数返回特定类型的值时,通常由基本类型变量(例如bool,int,char,string等)接收它。但是,对于ostreamistream ,返回的ostream&类型不是保存。 Then, in this case, shouldn't it be void (carry out the task and terminate without returning any values)? 然后,在这种情况下,它是否应该无效(执行任务并终止而不返回任何值)?

Because otherwise you would be able to chain the calls to operator<< . 因为否则您可以将调用链接到operator<< This: 这个:

cout << phone << endl;

is parsed as: 解析为:

(cout << phone) << endl;

and resolves as: 并解析为:

operator<<(cout, phone).operator<<(endl);

So it first calls operator<<(cout, phone) , which returns cout , which then allows the second << to call cout.operator<<(endl) . 因此,它首先调用operator<<(cout, phone) ,后者返回cout ,然后允许第二个<<调用cout.operator<<(endl)

If operator<< returned eg void , the second << would try to call operator<<(void, endl) which would not compile. 如果operator<<返回,例如void ,第二个<<将尝试调用不会编译的operator<<(void, endl)

"Why is the friend function returning ostream& ?" “为什么朋友函数返回ostream& ?”

To make this part of the call chain working 使呼叫链的这一部分起作用

phone << endl;

The ostream& reference is passed through all of these function calls, and thus the operator<<() function can be called again on the result. ostream&引用通过所有这些函数调用传递,因此可以在结果上再次调用operator<<()函数。

Why is the friend function returning ostream&? 为什么朋友函数返回ostream&?

To allow operators to be chained together. 允许将操作员链接在一起。 The operators return a reference to the same stream that was passed in. Without that return reference, you would not be able to call << endl on the return value of cout << phone . 运算符将引用返回给传入的同一流。没有该引用,您将无法在cout << phone的返回值上调用<< endl

I mean when a function returns a value of a particular type, it is generally received by a fundamental type variable such as bool, int, char, string, and etc. 我的意思是,当函数返回特定类型的值时,通常由基本类型变量(例如bool,int,char,string等)接收它。

That is optional. 那是可选的。 A return value is temporary and goes out of scope at the end of the statement, if it is not assigned to a variable to extend its lifetime. 如果未将返回值分配给变量以延长其寿命,则返回值是临时的,并且超出了语句结尾的范围。

However, for ostream and istream, the returned type of ostream& is not being saved. 但是,对于ostream和istream,不会保存返回的ostream&类型。

It does not have to be. 不一定是。 It just has to stay alive long enough for the next operator to be called on it, if the final ; 它仅需保留足够长的时间,以便下一个操作员(如果是最终操作员)可以被调用; has not been reached yet. 尚未达到。

Then, in this case, shouldn't it be void (carry out the task and terminate without returning any values)? 然后,在这种情况下,它是否应该无效(执行任务并终止而不返回任何值)?

No. 没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM