The using-declaration for inheriting constructors

57 views Asked by At

I'm strugling to understand why I'm getting a compilation error in the example below:

class Shape
{
protected:
    Shape() : num(0) {}
    Shape(int i) : num(i) {}
public:
    int getNum() const { return num; }
private:
    int num;
};

struct Lines : Shape
{
    using Shape::Shape;
};

int main()
{
    Lines l; // OK
    Lines l2(10); // error here
}

E0330 "Lines::Shape(int i)" (declared implicitly) is inaccessible C2248 'Lines::Lines': cannot access protected member declared in class 'Lines'

The actual code of this problem is from the book "“Programming: Principles and Practice Using C++, 2nd Edition” by B.Stroustrup:

struct Open_polyline : Shape { // open sequence of lines
    using Shape::Shape; // use Shape’s constructors (§A.16)
    void add(Point p) { Shape::add(p); }
};

class Shape { // deals with color and style and holds sequence of lines
public:
    void draw() const; // deal with color and draw lines
    virtual void move(int dx, int dy); // move the shape +=dx and +=dy
    void set_color(Color col);
    Color color() const;
    void set_style(Line_style sty);
    Line_style style() const;
    void set_fill_color(Color col);
    Color fill_color() const;
    Point point(int i) const; // read-only access to points
    int number_of_points() const;
    Shape(const Shape&) = delete; // prevent copying
    Shape& operator=(const Shape&) = delete;
    virtual ~Shape() { }
protected:
    Shape() { }
    Shape(initializer_list<Point> lst); // add() the Points to this Shape
    virtual void draw_lines() const; // draw the appropriate lines
    void add(Point p); // add p to points
    void set_point(int i, Point p); // points[i]=p;
private:
    vector<Point> points; // not used by all shapes
    Color lcolor {fl_color()}; // color for lines and characters (with default)
    Line_style ls {0};
    Color fcolor {Color::invisible}; // fill color
};

Open_polyline opl = {
    {100,100}, {150,200}, {250,250}, {300,200}
};
0

There are 0 answers