   Extending iostream to save certain file formats...
  I have a simple image class that I want to be able to save to different file formats.  I want to separate the different formats from the image class itself.  So I overloaded the << operator to serialize the data of the image to an std::ostream.I had a thought to write a class deriving from std::ostream that represents a certain format.  Here is how the syntax might look.image img;bmp_format bmp_file("image.bmp"); // bmp_format inherits std::ostreampng_format png_file("image.png"); // so does png_format...bmp_file << img; // Saves the image to a bmp file.png_file << img; // Saves the image to a png file.This way there are no dependencies between the bmp format and the image itself.  Then I could add different file formats to save to with minimal effort.  The only thing the format needs to know about is the structure of the serialized data of the image.Is this a horrible idea?  It seems like using iostreams to do this would be fairly common; however I am unable to find any examples of inheriting std::ostream through searching.Any suggestions?   
  I think it would work better if you just have some other base class, called ImageWriter or something, that has and uses a stream internally, and if you override the << operator for that ImageWriter baseclass.  It just seems more correct for me to do it that way and let the ImageWriter class control access to the stream than to override the << operator for the stream itself.   ;  While there is nothing wrong with having a class that saves images, I would most definately not overload operator<< for that class.First, you should only be overloading operators where it makes sense.  Secondly, operator<< is already used by the standard library to mean "formatted text output" and you're not doing formatted text output.Save yourself the confusion and just do something like:bmp_file output("foo.bmp");output.save(img);   ;Quote:Original post by c_olinIs this a horrible idea?  It seems like using iostreams to do this would be fairly common; however I am unable to find any examples of inheriting std::ostream through searching.In theory, it's not a horrible idea.In practice, C++ streams are the black sheep of the C++ standard library.  Boost has an entire library dedicated toward trying to make the standard streams more usable, and a huge chunk of that is geared towards trying to make streams customizable/sanely inheritable.  It's not worth the bother -- Implementing the idea would be the horrible thing here.If you want to have it work directly with streams (for aiding the creation of custom file systems?), I'd suggest just writing some free functions:std::ofstream file("image.png");write_as_png( file, img );If you do use a class (maybe you don't want to pass all the format/compression options as parameters?), something along the lines of what legalize suggests might be appropriate.  This lets you work with existing stream classes without the headache of trying to create your own.   ;Quote:Original post by c_olinIs this a horrible idea?  It seems like using iostreams to do this would be fairly common; however I am unable to find any examples of inheriting std::ostream through searching.Yes, it is a horrible idea.  The standard I/O streams are text formatting classes.  Trying to make binary serialization look and act like text formatting violates a number of tenets of good design.  Back to the drawing board for you.