Jump to content
  • Advertisement
Sign in to follow this  
REspawn

[c++] fstream default value in constructor

This topic is 3783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have an fstream in my constructor and want to give it a default value so it doesnt always have to be passed. I tried searching around and setting it to null or 0 with no luck.

Share this post


Link to post
Share on other sites
Advertisement
0) Do you really care that it's an fstream? For that matter, is it going to be an input stream, or an output stream (that is probably much more important)?

1) How exactly do you expect to read from or write to the "default" stream? If you are reading it, what does it contain, and why? If you are writing to it, where does the data go?

2) Have you considered instead passing a file name, and letting the constructor open the stream itself?

3) Or for that matter, if you simply have a totally different way of constructing the object when there is no available file data, have you considered simply overloading the constructor, instead of trying to make default parameters work? (Consider calling a common "helper" function to avoid repeated code. Note that calling constructors from inside constructors doesn't help initialize the current object; it only creates additional temporary objects of the same class.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
0) Do you really care that it's an fstream? For that matter, is it going to be an input stream, or an output stream (that is probably much more important)?

1) How exactly do you expect to read from or write to the "default" stream? If you are reading it, what does it contain, and why? If you are writing to it, where does the data go?

2) Have you considered instead passing a file name, and letting the constructor open the stream itself?

3) Or for that matter, if you simply have a totally different way of constructing the object when there is no available file data, have you considered simply overloading the constructor, instead of trying to make default parameters work? (Consider calling a common "helper" function to avoid repeated code. Note that calling constructors from inside constructors doesn't help initialize the current object; it only creates additional temporary objects of the same class.)


0 - Yes, if I didnt care why would I ask!?
1 - This function is passed an fstream for reading from only
2 - It does get passed a filename, if the stream is not open it opens one, thats why im asking what to set the default value to.
4 - Tought about that, but its not necessary

@VanillaSnake21 thanks it works

Share this post


Link to post
Share on other sites
Quote:
Original post by REspawn
0 - Yes, if I didnt care why would I ask!?


OK, now why do you care?

What I mean is, why shouldn't I be able to construct an object of your class by putting the appropriate data into a std::string, wrapping a std::istringstream around it, and passing the std::istringstream to the constructor?

Quote:
1 - This function is passed an fstream for reading from only


OK, now if you have a "default" fstream, what do you expect it to contain? Why?

Quote:
2 - It does get passed a filename, if the stream is not open it opens one, thats why im asking what to set the default value to.


Wait, so you pass a file name and a stream? And you might pass a stream object that isn't open? And then you want to ignore that stream and create a new one using the given file name?

What are you really doing? What is your object? What kind of data is in the file, and how does it help construct your object? You can't expect a proper solution without this kind of information...

Share this post


Link to post
Share on other sites
Its for a model loader, you may be the model from one file off the desktop, in which case you would just supply the file name, or in the other situation you would be supplying a stream to an archive (one large file that contains many files at different offsets).

This is the reason its set up like this.

Share this post


Link to post
Share on other sites
So you're never supplying both a file name AND a file? Use two constructors, one for file name and one for an open stream. Use common function to do the loading, and let the constructors only prepare and pass the parameters for the common function.

And I see no reason why the input streams have to be file streams, and not generic streams. So take a look at Zahlman's 0:th question again and ask yourself if there really is a reason you cannot allow any stream types other than file streams. I mean, in most common cases, this limitation is relaxed only by removing the 'f' in 'ifstream'.

Share this post


Link to post
Share on other sites
Quote:
Original post by VanillaSnake21
This seems to work
Constructor (fstream strm = fstream())
{
...
}

It shouldn't. std::fstream derives (eventually) from std::basic_ios<> which has a private copy constructor.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!