Should I use fstream.open()?

I found out that you can use "ofstream foo(path)" instead of:

1
2
ofstream foo;
foo.open(path);


Using "fc" in cmd I found out that the executables are different. Which one should I use?
The ofstream class has overloaded constructors:
1
2
ofstream foo(path); // calls initialization ctor
ofstream foo; // calls default ctor;  


You can see in the following link how the 2 ctor's vary: http://www.cplusplus.com/reference/fstream/ofstream/ofstream/

As to which one to call, unless the filename is going to be passed to various functions and therefore better off being assigned to a variable first, I'd suggest the initialization ctor
Last edited on
Using the default constructor makes most sense when you use the stream inside a class as a member variable.
Yes agreed. In fact, as a general principle, the further away ofstream is declared in the program from actual use the more sense it makes to use the default ctor rather than using the initialization ctor and then having to re-assigning it later
Topic archived. No new replies allowed.