How to compose a decorated class hierarchy without copying data

I have three classes that are related hierarchically:

  • Pattern

    which has a protected field of _panels

    type std::vector<Panel>

    .
  • Panel

    , in turn, has a protected field _edges

    that is of type std::vector<Edge>

    .
  • Edge

    finally has a protected field _verts

    that is of type std::vector<Eigen::Vector2f>

    .

I also have a pure virtual class Renderable

that has a pure virtual function virtual void render()

. I want to create specialized versions of each of my three classes that inherit from Renderable

, for example:

class VPattern : public Pattern, public Renderable
{
public:
    void render() { ... }
protected:
    ...
private:
    ...
}; // class VPattern

      

However, the field _panels

will contain instances Panel

, not VPanel

. This would mean that I would have to put the drawing logic for Edge

and Panel

into the paint function Pattern

, which is clearly not ideal.

Is there any other approach here that I can't see to avoid all this? Would using a wrapper class be a more appropriate approach?

+3


source to share


1 answer


Is there a reason why you cannot have a virtual class Drawable

that has a pure virtual function draw()

that all of its descendants will implement Drawable

?



public virtual struct Drawable {
    virtual void draw() = 0;
};

public struct Edge : Drawable {
    void draw() {}
};

//...

      

0


source







All Articles