-
fstream question
I have a question about the fstream class constructor:
Code:
explicit fstream ( const char * filename, ios_base::openmode mode = ios_base::in | ios_base::out );
How can the constructor argument be assigned?
Code:
= ios_base::in | ios_base::out
And what is "mode" itself? Is it a class of type openmode?
-
The "assignment" is creating a default parameter. If you only send one argument to the fstream constructor, then the second parameter will be defaulted to that value. Default parameters work for all functions, not just constructors.
mode is a function parameter of type ios_base::openmode. As far as you're concerned as a user of the function, that might be a class or it might be a typedef for a simple type like int, long or unsigned int. It doesn't really matter as long as you pass in the proper types of values.
-
Usually when using fstream you will only pass in the first parameter. The other params are there for more advanced usages but have been defaulted to default values b/c they are not used that often so it doesn't make sense to require the user to pass in params that 9 times out of 10 no one cares about or needs.