"using", protected constructors and initializer_list

Hi,

I'm working through Stroustrup's Programming book (2nd ed.), and in Chapter 13 he has a derived struct (Open_polyline) that makes direct use of its base class (Shape) constructors' (which are declared to be protected) with:

1
2
3
4
struct Open_polyline : Shape {
   using Shape::Shape;
   ...
}


OK, I get it. However, Shape has two constructors, namely:

1
2
  Shape();
  Shape(initializer_list<Point> lst);


and when I try to compile the code it fails for the 2nd constructor at run time with:

Graph.h:207:15: error: ‘Graph_lib::Open_polyline::Open_polyline(std::initializer_list<Graph_lib::Point>)’ is protected
using Shape::Shape;
^
graphics_examples/open_poly.cpp:14:5: error: within this context
};

However, if I get rid of that "using" statement and instead give the Open_polyline struct explicit calls to the base class constructors, i.e.:

1
2
3
4
5
struct Open_polyline : Shape { 
        Open_polyline() {};
        Open_polyline(initializer_list<Point> lst) : Shape(lst) {};
        ...
};


it now all works. The base class constructors are still declared protected. Can anyone shed some light while the first method (with the "using" statement) fails, whereas the second works.

It's interesting to note that Stroustrup expects the first method to work. I've tested under both G++ and clang++ with identical results.

TIA
dmc

> the first method (with the "using" statement) fails

The inherited protected constructor of the base class is a protected constructor in the derived class.

A using-declaration that names a constructor implicitly declares a set of inheriting constructors.
...
A constructor so declared has the same access as the corresponding constructor in (the base class). - IS



> whereas the second works.

Here, the base class constructors are not inherited; and the derived class constructors are declared as public
A constructor of the derived class can access the the protected constructor of the base class.
Many thanks for the reply.

dmc
Topic archived. No new replies allowed.