Home > Blockchain >  C multiple inheritance problem using FLTK
C multiple inheritance problem using FLTK

Time:12-08

i've a problem drawing basic shape with fltk.

I've made 2 classes'Rectangle' and 'Circle' that show normally. Then i've created a third class that inherit from 'Rectangle' and 'Circle' called 'RectangleAndCircle' :

//declaration in BasicShape.h
class Rectangle: public virtual BasicShape, public  virtual Sketchable{
    int w,h;
public:
    Rectangle(Point center, int width=50, int height=50, Fl_Color fillColor=FL_WHITE, Fl_Color frameColor=FL_BLACK);
    void setPoint(Point new_p){center=new_p;}
    virtual void draw() const override;
};

class Circle:public virtual BasicShape, public  virtual Sketchable{
    int r;
public:
    Circle(Point center, int rayon=50, Fl_Color fillColor=FL_WHITE, Fl_Color frameColor=FL_BLACK);
    virtual void draw() const override;
};

class RectangleAndCircle: public virtual Rectangle, public virtual Circle{
public:
    RectangleAndCircle(Point center,int w, int h, int r,
                       Fl_Color CircFillColor, Fl_Color CircFrameColor,
                       Fl_Color RectFillColor, Fl_Color RectFrameColor);
    void draw() const override;

When i try to draw a 'RectangleAndCircle' instance, the rectangle and the circle share the same color even if i the rectangle color is set.

Here is the code for the constructor of 'RectangleAndCircle' and the drawing of the shapes :

RectangleAndCircle::RectangleAndCircle(Point center, int w, int h, int r, Fl_Color CircFillColor,
                                       Fl_Color CircFrameColor, Fl_Color RectFillColor, Fl_Color RectFrameColor)
                                       :Rectangle(center,w,h,RectFillColor,RectFrameColor)
                                       , Circle(center,r,CircFillColor,CircFrameColor){}


void Rectangle::draw() const {
    fl_begin_polygon();
    fl_draw_box(FL_FLAT_BOX, center.x w/2, center.y h/2, w, h, fillColor);
    fl_draw_box(FL_BORDER_FRAME, center.x w/2, center.y h/2, w, h, frameColor);
    fl_end_polygon();
}

void Circle::draw() const {
    fl_color(fillColor);
    fl_begin_polygon();
    fl_circle(center.x, center.y, r);
    fl_end_polygon();
}

void RectangleAndCircle::draw() const {
    Rectangle::draw();
    Circle::draw();
}

I create a instance of 'RectangleAndCircle' in my MainWindow class, and i draw it.

RectangleAndCircle r{Point{50,50},50,50,12,FL_RED,FL_BLACK, FL_WHITE, FL_BLACK};
...
r.draw()

Am i doing something wrong ?

CodePudding user response:

You're using virtual inheritance. This means there will be only one instance of BasicShape in RectangleAndCircle. This BasicShape will have its fillColor set by both the Rectangle and Circle constructors, whichever being called last overwriting the value.

My advice would be to not inherit here, and instead have two fields of type Circle and Rectangle in RectangleAndCricle and then call draw on those separately in draw. Inherit to be reused, not reuse (you presumably wouldn't want to pass a RectangleAndCricle as a Circle or Rectangle)

  • Related