|
Operators and inheritance
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?
Snale
+--My humble and superior homepage
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:
Dire Wolf
www.digitalfiends.com
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:
|
Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
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
Snale
+--My humble and superior homepage
Edited by - snale on May 21, 2001 5:28:22 PM
Snale+--My humble and superior homepage
So you are saying images are supposed to be inserted and extracted to/from streams?
Dire Wolf
www.digitalfiends.com
Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
www.digitalfiends.com
Yes, you define a virtual print and read fonction in each of your classes :
|
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
Snale
+--My humble and superior homepage
Edited by - snale on May 22, 2001 5:48:09 AM
Snale+--My humble and superior homepage
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.
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
Snale
+--My humble and superior homepage
Snale+--My humble and superior homepage
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement