C++ 创建一个将 ostream 作为参数并写入该流的打印函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7179161/
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 16:33:46  来源:igfitidea点击:

Create a print function that takes an ostream as an argument and writes to that stream

c++operator-overloadingostream

提问by Bap Johnston

Im currently anwsering exercise questions concerning operator overloading in C++. I have a question:

我目前正在回答有关 C++ 中运算符重载的练习问题。我有个问题:

Create a simple class containing an int and overload the operator+ as a member function. Also provide a print( ) member function that takes an ostream& as an argument and prints to that ostream&. Test your class to show that it works correctly.

创建一个包含 int 的简单类并将 operator+ 作为成员函数重载。还提供一个print() 成员函数,该函数将ostream& 作为参数并打印到该ostream&。测试您的类以表明它可以正常工作。

I can create the class and write the operator+ function alright but I really dont understand the second part of the question. So far in my study of c++ I havent really come across ostream's and as such am not sure if its possible to explicitly create such a stream. I have tried using:

我可以创建类并编写 operator+ 函数,但我真的不明白问题的第二部分。到目前为止,在我对 c++ 的研究中,我还没有真正遇到过 ostream,因此我不确定是否可以明确创建这样的流。我试过使用:

std::ostream o;

std::ostream o;

However this produces an error. Could someone please enlighten me on how I should create this function please?

但是,这会产生错误。有人可以请教我应该如何创建这个功能吗?

回答by Mooing Duck

So far in my study of c++ I havent really come across ostream's and as such am not sure if its possible to explicitly create such a stream. I have tried using: std::ostream o;

到目前为止,在我对 c++ 的研究中,我还没有真正遇到过 ostream,因此我不确定是否可以明确创建这样的流。我试过使用:std::ostream o;

You must have missed something, because ostreams are important. std::cout is a variable of type std::ostream by the way. Usage is more or less like this

你一定错过了一些东西,因为 ostreams 很重要。顺便说一下,std::cout 是一个 std::ostream 类型的变量。用法或多或少是这样的

#include <iostream> //defines "std::ostream", and creates "std::ofstream std::cout"
#include <fstream> //defines "std::ofstream" (a type of std::ostream)
std::ostream& doStuffWithStream(std::ostream &out) { //defines a function
    out << "apples!";
    return out;
}
int main() {
    std::cout << "starting!\n"; 
    doStuffWithStream(std::cout); //uses the function

    std::ofstream fileout("C:/myfile.txt"); //creates a "std::ofstream"
    doStuffWithStream(fileout); //uses the function

    return 0;
}

回答by Benjamin Lindley

You don't create an ostream, you create an ostream reference, like your exercise question said. And you do it in the parameter list of your print function, i.e.

您不创建 ostream,而是创建 ostream 引用,就像您的练习题所说的那样。然后在打印函数的参数列表中执行此操作,即

void print(std::ostream & os);

Then you can call that function, passing cout or any other object of a class derived from ostream(ofstream, ostringstream, etc...)

然后您可以调用该函数,传递 cout 或从 ostream(ofstream, ostringstream, etc...) 派生的类的任何其他对象