Advertisement

Advanced classes? Or do I need a friend..

Started by January 07, 2001 05:47 PM
5 comments, last by gimp 24 years ago
I have a class, call it CImage. I''d like CImage to hold information about images in a fairly independent format (that agree''s with opengl for example). When I crate an image onject in my game, I''d like to call Image.Load(blah) and have image work out the extention for the type. Then image should use say CPNGLoader to populate it''s member variables with the image data and image information (height, width, bitdepth, alpha...). How should I do this last part? Without passing 7-8 references to CPNGLoader, how can it populate CImage with the data? Is this waht friend classes were meant for? Many thanks Chris
Chris Brodie
Well, I suppose you could always pass a reference to your CImage itself into CPNGLoader for population.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Advertisement
I kind of guess that this was the answer. I was just curious to see if an object could inherit other classes at runtime, sort of like "I give you a key, now your available options include open door, etc."... guess not huh?
Chris Brodie
You could use member functions instead of factories, or make a base class CImageLoader that sucked data out of the file in chunks and you tell the ImageLoader what to give you.

CImageLoader* pImageLoader;
pImageLoader = ImageLoaderFactory(".jpg");
pImageLoader->Open("water.jpg");
pImageLoader->GetDesc(&SomeDescRecord);
then set the CImage members equal to the corresponding values of the DescRecord...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
You could always pass a reference to a structure.

OR

You could just map CImage''s functions to CPNGLoader

i.e. when you call a CImage.Load("filename.png");
the CImage figures out that you want to load a png file, which then declares a pointer to a new CPNGLoader (which is inherited from CIMAGELoader). Then whenever you want to use a CImage function like CImage.GetHeight() for example you do this:

int CImage::GetHeight()
{
return CImageLoader->GetHeight();
}



Regards,
Nekosion
Regards,Nekosion
Sounds like you want to use a factory method:

static CImage *CImage::loadImage (const char* filename){   // ImageType is an enum of all of your supported file types   ImageType type = getTypeFromExtension (filename);   switch (type)   {   case IMAGE_TYPE_PNG: return CPNGLoader::load (filename);   case IMAGE_TYPE_BMP: return CBMPLoader::load (filename);   /*...*/   default: throw runtime_error ("unsupported file type");   }   assert (0);  // compiler not smart enough to know we''ll never   return NULL; // reach here, so we have to return NULL} 


CImage::loadImage is your factory method, that will call other classes that return new CImage objects. I assumed that CPNGLoader::load (const char *filename) is also a static function, and that it basically creates a new CImage, fills it with file data, and returns it.
Advertisement
Thanks for idea''s guys... I thought about a factory but ended up deciding for 3-4 types it was a bit excessive.

I''ve gone with tight binding as fel suggested, passing a *this. (Mostly beacuses it was the most attractive option yesterday.

Nekosion also had a nice simple idea of just remapping the function calls.

Thanks all

Chris
Chris Brodie

This topic is closed to new replies.

Advertisement