[英]What is the purpose of returning an IO object in C++?
I am attempting to teach myself C++ by reading through a textbook and doing practice problems and stuff, and the topic I am currently learning is a little confusing to me and I am hoping to get some clarification.我正在尝试通过阅读教科书并做练习题和其他东西来自学 C++,我目前正在学习的主题对我来说有点混乱,我希望得到一些澄清。 I have looked online for a clear answer to my question, but have not yet found anything.
我在网上寻找我的问题的明确答案,但还没有找到任何东西。
I am currently learning the details of IO Classes in the standard library, and the section I am on right now gives some examples that has functions that pass and return IO objects.我目前正在学习标准库中 IO 类的细节,我现在所在的部分给出了一些具有传递和返回 IO 对象的函数的示例。
For example:例如:
istream &get_value(istream &input)
{
int value;
input >> value;
return input;
}
int main()
{
get_value(cin);
return 0;
}
I understand on a high-level view what is happening here.我从高层次的角度理解这里发生的事情。 The
get_value
function has a reference to an input object type and it also takes in a reference to an input object, which in my example I used to commonly used cin
object. get_value
函数有一个对输入对象类型的引用,它还接受对输入对象的引用,在我的例子中,我经常使用cin
对象。 I get that this function is reading input from the user in the console and is storing that input as value
.我知道这个函数正在从控制台中读取用户的输入并将该输入存储为
value
。
What I do not understand is what the reason for returning the input object is.我不明白的是返回输入对象的原因是什么。 Why shouldn't this function have a type
void
?为什么这个函数不应该有一个类型
void
? What could the input object I am using be used for?我正在使用的输入对象可以用来做什么? I know I am not using it for anything right now, but what could it be used for?
我知道我现在没有用它做任何事情,但它可以用来做什么?
The return value is so you can "chain" the calls between the stream operators <<
and >>
.返回值使您可以“链接”流运算符
<<
和>>
之间的调用。 Operator overloading is a good motivation for this "chaining".运算符重载是这种“链接”的一个很好的动机。
using namespace std;
class book {
string title;
string author;
public:
book(string t, string a) : title(t), author(a) { }
friend ostream &operator<<(ostream &os, const book &x);
}
ostream &operator<<(ostream &os, const book &x)
{
os << x.title << " by " << x.author << "\n";
return os;
}
int main()
{
book b1 { "Around the World in 80 Days", "Jules Verne" };
book b2 { "The C Programming Language", "Dennis Ritchie" };
cout << b1 << b2; // chaining of operator<<
}
If operator<< didn't return an ostream, we would not be able to pass the modified ostream from the first operator<< to the second one.如果 operator<< 没有返回 ostream,我们将无法将修改后的 ostream 从第一个 operator<< 传递到第二个。 Instead we would have to write
相反,我们将不得不写
cout << b1;
cout << b2;
The same applies for input operations, like in your case, with >>
这同样适用于输入操作,就像你的情况一样,
>>
To use it again to store another var stored in the buffer.再次使用它来存储另一个存储在缓冲区中的 var。 Like
get_value(get_value(cin,v1),v2);
像
get_value(get_value(cin,v1),v2);
#include<iostream>
std::istream &get_value(std::istream &input,int& value)
{
input >> value;
return input;
}
int main()
{
int v1{}, v2 {};
std::cout<<"Enter two succesive integers: ";
get_value(get_value(std::cin,v1),v2);
std::cout<<"\nThe two input integers are "<<v1<<" and "<<v2;
return 0;
}
You could use it to get another input value;您可以使用它来获取另一个输入值; for example:
例如:
input >> value >> value2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.