Does passing (ostream &os) to print a simple object like an array add any value?
Jul 12, 2018 at 5:06am UTC
Whyuse void printa(ostream &os,int *Array) instead of just printa(int *Array) which does the exact same thing?
1 2 3 4 5 6 7 8 9 10 11 12 13
void printa(int *a)
{ for (...)
cout<< a[i]<<endl;
}
int main()
{
int *a= new int [10];
printa(a);
delete a;
}
1 2 3 4 5 6 7 8 9 10 11
void print_array10(ostream& os, int * a) {
for (...)
os << a[i] << "\n" ;
}
int main()
{
int *a= new int [10];
print_array10(cout,a)
I learned that you can print to a file using the second version, but besides that any benefit?
Last edited on Jul 12, 2018 at 5:10am UTC
Jul 12, 2018 at 5:32am UTC
Passing a stream object allows for changing the stream, so the same function that outputs to the console can output to a file by changing the passed stream.
Topic archived. No new replies allowed.