Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a function in c++ to output to designated source

I want to have a function that outputs certain pieces of information to a specific designated source that is inputted to the function. In code, what I mean is:

function output( source ) {

source << "hello" << endl;

}

where source can be a ofstream or cout. So that I can call this function like so:

output(cout) or ofstream otp ("hello"); output(otp)

My question is, how do I characterize source to make this work? It's fair to assume that source will always be a member of the std class

Thanks!

like image 649
Amit Avatar asked Dec 13 '22 09:12

Amit


1 Answers

void output(std::ostream &source) {
    source << "hello" << std::endl;
}

or even:

template <T>
void output(T &source) {
    source << "hello" << std::endl;
}
like image 163
Bill Lynch Avatar answered Dec 14 '22 21:12

Bill Lynch