I know that generally, references to data should be isolated as a constant within a function so that the function can't change it, does the same hold true for fstream objects that are only being used for input?
Such as...
void doFoo(fstream &fileName)
{
fileName.open("data.txt", ios::in);
}
IF is it advisable, does it follow the same logic of most everything else?
Such as...
void doFoo(const fstream &fileName)
{
fileName.open("data.txt", ios::in);
}
also curious about output streams as well
I'm just wondering if it matters, and if so, why?
thanks!
The constantness of a file does not translate to the constant attribute of the object used to access the file. Even for just reading a file, you must modify internal buffers, the current read position etc. The same applies to output, so basically a const stream is not useful.
BTW: If you want to make clear that a function only reads from a stream, pass it a std::istream&
. Firstly, the "i" in istream
makes reasonably sure that you don't write to it. Secondly, the missing "f" of fstream
allows the use with stringstreams, too, or streams like cin.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With