Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

A Design question about std::ofstream

This topic is 2808 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

What justification is there for this code to be an error :

std::ofstream fileOut = std::ofstream("file.dat");


The error says that the copy constructor std::ios_base. Anyone know the reasoning behind this? Again, it is well know that late night is not my brightest hours, so forgive me if this is a simple question that I overlooked.

Share this post


Link to post
Share on other sites
Advertisement
You're creating 2 streams and attempting to copy one to the other. Try std::ofstream fileOut("file.dat")

Share this post


Link to post
Share on other sites
It's weird because it seems equivalent to:


int x = int(5);


which works fine. Maybe someone else can explain?

edit: I just tried your code and it compiles fine for me using vs 2010. Which compiler are you using?

Share this post


Link to post
Share on other sites
What does it mean to copy a stream? What if the stream is buffered - how should that be handled. Should copying a file stream result in two files? Probably not, but how do we handle interleaved reads and writes? Streams are non-trivial objects, with complex state. You can create your own streambuf implementation, how should it be copied? It could point at a network device, a randomly generated byte stream, anything.

The simplest solution is to disable copying in such cases. C++ streams are noncopyable, which in C++ is done by marking the copy constructor and assignment operator as "private".

@bibalasvegas, in that case the integer is copyable, so the code is fine.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!