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

37,069

Solution 1

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;

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

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

Solution 2

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.

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

Share:
37,069
Bap Johnston
Author by

Bap Johnston

Updated on August 10, 2022

Comments

  • Bap Johnston
    Bap Johnston 4 months

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

    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.

    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:

    std::ostream o;

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

  • Bap Johnston
    Bap Johnston over 11 years
    Thank you for your help. Normally when passing an argument you need an object to pass which is why I was confused by this question, I thought I had to create an object of type ostream.
  • Bap Johnston
    Bap Johnston over 11 years
    I do understand that cout is type ostream, however as iv never had to pass ostream as a function argument I was unsure how to do it. I thought to have a reference to an ostream as an argument in a function I had to create an ostream object first, seems this is not the case. Thanks for helping to clear this up.
  • Mooing Duck
    Mooing Duck over 11 years
    A function definition doesn't require any objects to actually exist yet, it only needs to know about types. Obviously, you have to create a stream at some point to pass to the function. Here, I pass std::cout (which is automatically created when you include <iostream>), and fileout, which I create myself.