C++ 将 ostream 作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5507924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:19:18  来源:igfitidea点击:

C++ Passing ostream as parameter

c++parameter-passingostream

提问by ChrisM

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.

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

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

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;
}

instead of something like this:

而不是这样的:

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;
}

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 作为参数,或者缺少其他一些明显的原因来将 ostream 传递到流中?

回答by Billy ONeal

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

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

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::stringstreamfor this, and the function you're working with is none the wiser.

这通常用于测试之类的事情。假设您想要正常的控制台输出,因此您将传递对std::cout. 但有时您想要进行测试,例如单元测试或验收测试,并且您想要将输出存储在内存中。您可以std::stringstream为此使用它,而您正在使用的功能也不是那么明智。

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.

这是一个特定的情况——但一般来说,在任何你想要改变数据源或接收器可能来自/去向的地方,你都可以通过传递一个流来做到这一点。

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

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

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

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

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

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.
}

回答by Lee

I would just overload the <<operator:

我只会重载<<运算符:

class Card{
public:
    friend ostream& operator<<(ostream& os, const Card& s);
};

ostream& operator<<(ostream& os, const Card& s){
    os << "Print stuff";
    return os;
}

And you could overload in the Rolodex as well to just iterate over the cards.

您也可以在 Rolodex 中重载以遍历卡片。