在c ++中创建一个函数以输出到指定的源

Ami*_*mit 4 c++ iostream class

我想要一个功能,将某些信息输出到输入到该功能的特定指定源.在代码中,我的意思是:

function output( source ) {

source << "hello" << endl;

}
Run Code Online (Sandbox Code Playgroud)

来源可以是ofstreamcout.所以我可以像这样调用这个函数:

output(cout) 要么 ofstream otp ("hello"); output(otp)

我的问题是,我如何表征source这项工作?可以假设,这source将永远是std班上的一员

谢谢!

Bil*_*nch 7

void output(std::ostream &source) {
    source << "hello" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

甚至:

template <T>
void output(T &source) {
    source << "hello" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)