Question about design (inheritance, polymorphism)

Posted by Dan on Stack Overflow See other posts from Stack Overflow or by Dan
Published on 2010-04-09T20:55:04Z Indexed on 2010/04/09 21:13 UTC
Read the original article Hit count: 320

Hi,

I have a question about a problem I'm struggling with. Hope you can bear with me.

Imagine I have an Object class representing the base class of a hierarchy of physical objects. Later I inherit from it to create an Object1D, Object2D and Object3D classes. Each of these derived classes will have some specific methods and attributes. For example, the 3d object might have functionality to download a 3d model to be used by a renderer.

So I'd have something like this:

class Object {};
class Object1D : public Object { Point mPos; };
class Object2D : public Object { ... };
class Object3D : public Object { Model mModel; };

Now I'd have a separate class called Renderer, which simply takes an Object as argument and well, renders it :-) In a similar way, I'd like to support different kinds of renderers. For instance, I could have a default one that every object could rely on, and then provide other specific renderers for some kind of objects:

class Renderer {};  // Default one
class Renderer3D : public Renderer {};

And here comes my problem. A renderer class needs to get an Object as an argument, for example in the constructor in order to retrieve whatever data it needs to render the object.

So far so good. But a Renderer3D would need to get an Object3D argument, in order to get not only the basic attributes but also the specific attributes of a 3d object.

Constructors would look like this:

CRenderer(Object& object);
CRenderer3D(Object3D& object);

Now how do I specify this in a generic way? Or better yet, is there a better way to design this?

I know I could rely on RTTI or similar but I'd like to avoid this if possible as I feel there is probably a better way to deal with this.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about design

Related posts about design-patterns