N
nebjy
Hi,
I've got a problem where I want to have an interface to a Graphics
class (though the nature of the class isn't relevant to the problem),
and I want to have a method that returns a pointer to a different
object, depending on the implementation of the Graphics object. I'm
not sure how to do this, whether a typedef would do it or if I need
a template. As an example, I might implement Graphics with the SDL
Library, then the object would be an SDL_Surface, but I was hoping
I could have a method that returned a generic SURFACE thing of my
devising.
Here's some code which I wrote, to try to explain. However it's
verging on pseudocode because I don't know how to go about it.
*****
//interface
class Graphics
{
public:
Graphics();
~Graphics();
//a "made-up" generic SURFACE type
SURFACE getImage(char* filename);
}
Graphics g;
SURFACE *imgTree;
imgTree = g.getImage("myimage.png");
*****
And the implementation would be somewhere, returning an SDL_Surface
as a SURFACE, then I might have a
typdef SURFACE SDL_surface
somewhere? I don't know, I'm a bit lost, and the code above is probably
completely wrong, so you can take that as given when replying!
There'd only ever be one type of Graphics class at any time, I wouldn't
mix types. The main reason for the abstraction is so that
Any tips would be much appreciated.
I've got a problem where I want to have an interface to a Graphics
class (though the nature of the class isn't relevant to the problem),
and I want to have a method that returns a pointer to a different
object, depending on the implementation of the Graphics object. I'm
not sure how to do this, whether a typedef would do it or if I need
a template. As an example, I might implement Graphics with the SDL
Library, then the object would be an SDL_Surface, but I was hoping
I could have a method that returned a generic SURFACE thing of my
devising.
Here's some code which I wrote, to try to explain. However it's
verging on pseudocode because I don't know how to go about it.
*****
//interface
class Graphics
{
public:
Graphics();
~Graphics();
//a "made-up" generic SURFACE type
SURFACE getImage(char* filename);
}
Graphics g;
SURFACE *imgTree;
imgTree = g.getImage("myimage.png");
*****
And the implementation would be somewhere, returning an SDL_Surface
as a SURFACE, then I might have a
typdef SURFACE SDL_surface
somewhere? I don't know, I'm a bit lost, and the code above is probably
completely wrong, so you can take that as given when replying!
There'd only ever be one type of Graphics class at any time, I wouldn't
mix types. The main reason for the abstraction is so that
Any tips would be much appreciated.