473,325 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Class with method that returns different pointer depending on implementation?

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.

--
Nick H
Jul 22 '05 #1
7 1142
nebjy wrote:
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.


The usual way to do this is by use of _polymorphism_. You create your
'SURFACE' type, and any other surface you can think of should inherit
that type:

class SURFACE
{
// declarations that represent interface, usually abstract one
};
....
class Graphics
{
...
SURFACE* getImage(...
};
....
class SDL_surface : public SURFACE
{
};

Get a decent book that describes the use of inheritance and polymorphism
in C++, I recommend "Advanced C++" by James Coplien (although some may
consider it a bit old). Don't get discouraged by strange words and new
terms, they just make it look convoluted, in reality it's pretty straight-
forward and once you get the hang of it, it's easy and basically becomes
natural to an OO programmer.

Victor
Jul 22 '05 #2
Victor Bazarov wrote:
nebjy wrote:
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.

The usual way to do this is by use of _polymorphism_. You create your
'SURFACE' type, and any other surface you can think of should inherit
that type:
...
class Graphics
{
...
SURFACE* getImage(...
};
...
class SDL_surface : public SURFACE
{
};


I understand you and I get inheritence generally, but I don't think it's
a solution in this case.

The thing is that SDL_Surface is already implemented in SDL, and a
different implementation (say, with openGL) almost certainly wouldn't
share the same methods and data. As a result I can't create a common
SURFACE interface, and I can't define SDL_surface myself. I wouldn't
actually want to access
data or methods of the SURFACE, I'd just want a direct pointer to it
so I could then pass it to, say, a drawImage(SURFACE s) method to
draw it on the screen or something.

I guess I can just mess around with a typedef and hope for the best :)
I'm not too worried about adjusting a couple of lines and recompiling
if I need to change the implementation to something different.

Thanks for the reply.
Jul 22 '05 #3
nebjy wrote:
Victor Bazarov wrote:
nebjy wrote:
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.
The usual way to do this is by use of _polymorphism_. You create your
'SURFACE' type, and any other surface you can think of should inherit
that type:
...
class Graphics
{
...
SURFACE* getImage(...
};
...
class SDL_surface : public SURFACE
{
};

I understand you and I get inheritence generally, but I don't think it's
a solution in this case.

The thing is that SDL_Surface is already implemented in SDL, and a
different implementation (say, with openGL) almost certainly wouldn't
share the same methods and data. As a result I can't create a common
SURFACE interface, and I can't define SDL_surface myself. I wouldn't
actually want to access
data or methods of the SURFACE, I'd just want a direct pointer to it
so I could then pass it to, say, a drawImage(SURFACE s) method to
draw it on the screen or something.


Then what you're asking about is a wrapper (or wrappers) of those
things. You still can make it generic and have derived classes
implement (contain) other (predefined) classes:

class SURFACE_wrapper {
public:
virtual void blah() = 0;
};

class SDL_surface_wrapper : public SURFACE_wrapper {
SDL_surface thing;
public:
void blah();
};
I guess I can just mess around with a typedef and hope for the best :)
Whatever suits you.
I'm not too worried about adjusting a couple of lines and recompiling
if I need to change the implementation to something different.


That's up to you, of course.

Good luck!

V

Jul 22 '05 #4
Victor Bazarov wrote:
nebjy wrote:
Victor Bazarov wrote:
nebjy wrote:

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.


The thing is that SDL_Surface is already implemented in SDL, and a
different implementation (say, with openGL) almost certainly wouldn't
share the same methods and data.

Then what you're asking about is a wrapper (or wrappers) of those
things. You still can make it generic and have derived classes
implement (contain) other (predefined) classes:


That looks promising, I may give that a go, thanks!

Nick
Jul 22 '05 #5
Victor Bazarov wrote

Then what you're asking about is a wrapper (or wrappers) of those
things. You still can make it generic and have derived classes
implement (contain) other (predefined) classes:

class SURFACE_wrapper {
public:
virtual void blah() = 0;
};

class SDL_surface_wrapper : public SURFACE_wrapper {
SDL_surface thing;
public:
void blah();
};


Oh, just one question. What does it mean to say virtual void blah() = 0?
In particular, the "= 0" part. I've never seen an assignment on a
method declaration.
Jul 22 '05 #6
nebjy wrote:
Victor Bazarov wrote

Then what you're asking about is a wrapper (or wrappers) of those
things. You still can make it generic and have derived classes
implement (contain) other (predefined) classes:

class SURFACE_wrapper {
public:
virtual void blah() = 0;
};

class SDL_surface_wrapper : public SURFACE_wrapper {
SDL_surface thing;
public:
void blah();
};


Oh, just one question. What does it mean to say virtual void blah() = 0?
In particular, the "= 0" part. I've never seen an assignment on a
method declaration.


the "= 0" part indicates a pure virtual function. That is,
SURFACE_wrapper is an abstract class that cannot be instantiated
directly, but is intended as an interface.
Jul 22 '05 #7
red floyd wrote:
nebjy wrote:
Victor Bazarov wrote
virtual void blah() = 0;


Oh, just one question. What does it mean to say virtual void blah() = 0?
In particular, the "= 0" part. I've never seen an assignment on a
method declaration.


the "= 0" part indicates a pure virtual function. That is,
SURFACE_wrapper is an abstract class that cannot be instantiated
directly, but is intended as an interface.


Ah okay, thanks.
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
3
by: dischdennis | last post by:
Hello List, I would like to make a singleton class in python 2.4.3, I found this pattern in the web: class Singleton: __single = None def __init__( self ): if Singleton.__single: raise...
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: David Sanders | last post by:
Hi, I have a class depending on a template parameter: template<int n> class MyClass { }; I want the template parameter to be chosen according to a variable, e.g.
8
by: oh.i.love.spam | last post by:
I've been a procedural PHPer for a while now and I don't know why it has taken me so long to start the jump from procedural to OOP. I have a function that I would use for doing length...
16
by: Stefano Sabatini | last post by:
Hi all, I'm facing this design problem. I have a table which defines the behaviour of an Object in a given state according to the type of event it is receiving. So for each couple...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.