<分区>
我正在尝试实现一个类 Union
直接继承自类 Shape
(Union
是由多个形状组成的形状)。
Shape
的( protected )构造函数需要 Point
作为输入(代表形状的中心)。构建一个 Union
对象,唯一的输入是形状列表(const vector<const Shape>
)。实现 Union
的构造函数,我想使用初始化列表,如下所述
class Union : Shape
{
public:
Union(const std::vector<const Shape> shapes):
Shape(shapes[0].get_center()), shapes(shapes) {};
...
private:
const std::vector<const Shape> shapes;
}
与 get_center()
类 Shape
的 protected 虚函数.
class Shape
{
protected:
Shape (const Point& center) : center(center) {};
virtual const Point& get_center() const =0;
...
private:
const Point center;
}
但是,当我调用 get_center()
在 Union
的初始化列表中构造函数,有一个错误说“get_center() 是 Shape
的 protected 成员”。
谁能解释一下为什么我不能调用 get_center()
来自子类 Union
(应该是哪个继承了功能)?
谢谢!
P.S.: 如果我设置函数 get_center()
公开,就没有错误了。