Advertisement

Operators and inheritance

Started by May 21, 2001 03:48 PM
6 comments, last by Snale 23 years, 8 months ago
I recently finished a simple image class and got the idea that I could make child classes for different image file formats and just redefine the load function. Everything works quite well except for the << and >> operators I defined for loading and writing the images. They can''t be inherited since they aren''t memebers of Image, but I can''t make them members of Image since the first argument must be a stream object... Is there anyway around this other than copy and paste the defined operators into every child class?
  
//The base class


class Image 
	{
		public:
			int x, y, bpp;
			unsigned char* data;
			bool fubar;
		
			Image();
			//Image( const char* path );

			Image( const Image& I );

			~Image();
			virtual void load( const char *path );

			bool bad() const;
			
			friend ostream& operator<<( ostream& out, const Image& I );
			friend istream& operator>>( istream& in, Image& I );
	};
[\source]

  
Snale +--My humble and superior homepage
So you are trying to load an image into a stream because that is what you are trying to define.

I''d have guess that you wanted something more like:

image << "c:\\test.tga";
image >> "c:\\test.tga";

All you need to do is redefine your class like so:

  class Image 	{		    public:        int x, y, bpp;				unsigned char* data;	bool fubar;	Image() {}			//Image( const char* path );	Image( const Image& I ) {}       ~Image() {}	   	virtual void load( const char *path ) {}	bool bad() const {}			Image& operator<<( const char* file ) { return *this; }	Image& operator>>( const char* file ) { return *this; }};  




Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
Thats not what I want since the Image class is part of a greater set of classes that can load and view models etc. I want regular insertion and output operators because I store the image data in the model file.

Snale
+--My humble and superior homepage

Edited by - snale on May 21, 2001 5:28:22 PM
So you are saying images are supposed to be inserted and extracted to/from streams?



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Yes, you define a virtual print and read fonction in each of your classes :

  class Base{public:  virtual ostream& print( ostream& o );  virtual istream& read(  istream& i );};//define extrator only for base classostream& operator<<(ostream& os, Base& b){     return b.print(os);}istream& operator>>(istream& in, Base& b ){   return b.read(in);}class Derive:public Base{public: virtual ostream& print( ostream& ); //redefine as will virtual istream& read( istream& ); //redefine as will};  


Ahh... thanks, that was exactly the solution I was looking for!

Snale
+--My humble and superior homepage

Edited by - snale on May 22, 2001 5:48:09 AM
Advertisement
I want to add something...
I noticed that you said that you would derive the image to change the behavior of the loading and storing of images to support different formats...

Maybe that is not such a good idea, since you will be creating a hierarchy of images that differs only in the way they load (or store) an image. What if you want to create a specific-purpose image like "AnimatedImage", that can load animations from all formats?. You would have an explosion of classes.

Perhaps you should try a different approach to this design...
You could create a hierarchy whose resposibility is the handling of loading/storing images.

Something like this:

class IImageLoader
{
void Load(IStream*)=0
void Store(IStream*)=0
}

class GifLoader : public IImageLoader
{
// this class handles the loading of .gif
}

Probably you would need to give this class access to the data
members of image.

What you gain in this design is that you have only a Image class
,which you can subclass for more purpose-specific functionality, and that you can add support for new formats whithout changing any client code, or the Image class hierarchy.

Hope it helps

Daniel.
Not a bad idea at all. When I increase the number of supported formats i''ll keep that in mind

Snale
+--My humble and superior homepage

This topic is closed to new replies.

Advertisement