OO question

Started by
10 comments, last by NIm 17 years, 7 months ago
Declaring a friend is almost the same as declaring things highly coupled. The point of having a Renderable interface is that you can hide behavior behind it, and have different things render themselves differently without anyone else knowing about it. If you just make Renderable a friend of Display, you suddenly make Display ultra-smart by putting all logic of rendering things in one class instead of spreading it around on a need-to-know basis.


It's possible that you don't need to redesign all that much if you're turning things around, having the Renderable render itself on the Display, just add an extra indirection:
void Display::render(Renderable* renderable){    renderable->render(this);}class Renderable{public:    virtual void render(Display* display) = 0;};class Mesh : public Renderable{public:     virtual void render(Display* display) { display->renderMesh(my_mesh); }private:    Mesh* my_mesh;};class Composite : public Renderable{public:    Composite(Renderable* r1, Renderable* r2) : r1(r1), r2(r2) { }    virtual void render(Display* display)    {        r1->render(display);        r2->render(display);    }private:    Renderable *r1, *r2;};

Using this code, you can have Renderables which are actually composites or use some other logic than just rendering a mesh (see LorenzoGatti's post). You could see it as defining a whole new language for defining renderable stuff: composing, transforming, ... with each operation represented by a class.
Advertisement
Thank you for your help. I really appreciate it. I think, for what I plan to do with my code, making display all-knowledgable is what I want. renderables don't know anything about displays and drawing, so I'd like to confine them to information about the appearanc of something, and confine display to knowledge of how to draw things. I really appreciate you clarifying that for me, and I thank you for your smaple code. It's really helpful, and I learn alot by it, but it's not right for this project. Thanks.

This topic is closed to new replies.

Advertisement