C++ 将 ostream 作为参数传递 [英] C++ Passing ostream as parameter

查看:55
本文介绍了C++ 将 ostream 作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个虚拟 rolodex 做作业项目,该项目需要一个主类、一个 rolodex 类和一个卡片类.要将所有卡片"的内容输出到控制台,赋值语句说 main() 应该调用 rolodex 类中的 show(...) 函数,将它传递给一个 ostream 和 show(...) 然后迭代在卡片上,调用它们的每个 showCard() 函数.实际显示由卡片对象的 showCard() 函数完成,显示在提供的 ostream 上.

I'm working on a homework project for a virtual rolodex that has called for a main class, a rolodex class, and a card class. To output the contents of all of the "cards" to the console, the assignment says that main() should call a show(...) function in the rolodex class, passing it an ostream and show(...) then iterates over the cards, calling each of their showCard() functions. The actual showing is done by the card objects' showCard() function, showing on the provided ostream.

我不明白的是为什么 ostream 会/应该被传递到任何地方.似乎任务要求这样的事情:

What I don't understand is why an ostream would/should be passed anywhere. Seems like the assignment is calling for something like this:

main() {
   Rolodex myRolodex; 
   ostream myStream; 
   myRolodex.show(myStream); 
}

void Rolodex::show(ostream& theStream) {
   //for each card 'i' in the Rolodex...
   myCard[i].show(theStream);
}

void Card::show(ostream& theStream) {
   theStream << "output some stuff" << endl;
}

而不是这样的:

main() {
   Rolodex myRolodex;  
   myRolodex.show(); //no ostream passed 
}

void Rolodex::show() {
   //for each card 'i' in the Rolodex...
   myCard[i].show();//no ostream passed
}

void Card::show() {
   cout << "output some stuff" << endl;
}

我是不是误解了使用 ostream 作为参数,或者错过了像这样将 ostream 传递到流中的其他明显原因?

Am I either misunderstanding the use of ostream as a parameter or missing some other obvious reason to pass an ostream down the stream like that?

推荐答案

我不明白的是为什么 ostream 会/应该被传递到任何地方.

What I don't understand is why an ostream would/should be passed anywhere.

这通常用于测试之类的事情.假设您想要正常的控制台输出,那么您将传递对 std::cout 的引用.但有时你想做测试,例如单元或验收测试,并且您希望将输出存储在内存中.为此,您可以使用 std::stringstream,而您正在使用的函数并不明智.

This is often used for things like testing. Say you want console output normally, so you'd pass around a reference to std::cout. But sometimes you want to do testing, e.g. unit or acceptance testing, and you want to store the output in memory for that. You could use std::stringstream for this, and the function you're working with is none the wiser.

这是一种特殊情况——但一般来说,在任何您想要更改数据源或接收器可能来自/去往何处的地方,您都可以通过传递流来实现.

That's one specific case -- but in general any place where you'd want to change where the data source or sink could be coming from / going to, you can do that by passing a stream around.

例如,以下内容会将您的 rolodex 打印到控制台:

For example, the following would print your rolodex to the console:

int main()
{
    Rolodex myRolodex;
    myRolodex.show(std::cout);
}

...但是如果明天你想写入一个文件,你可以在不影响 Rolodex 内部代码的情况下这样做:

... but if tomorrow you wanted to write to a file instead, you can do that without affecting the code inside Rolodex at all:

int main()
{
    Rolodex myRolodex;
    std::ofstream file("This\\Is\\The\\Path\\To\\The\\File.txt");
    myRolodex.show(file); // Outputs the result to the file,
                          // rather than to the console.
}

这篇关于C++ 将 ostream 作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆