我有以下情况,我声明了一个超类const
的成员,现在我想在其子类之一的构造函数中使用列表初始化器对其进行初始化。
struct Shape {
public:
const Rect boundingRect; // the rect in which the shape is contained
};
struct Stain : Shape
{
public:
Stain(Rect boundingRect_) : boundingRect(boundingRect_) {}
};
我不确定这是否可能,如果我采用上面显示的直接方法,编译器会抱怨以下消息:
member initializer 'boundingRect' does not name a non-static data member or base class
这个答案解释了为什么不能在子类构造函数的列表初始化器中初始化超类的成员变量。我想知道这种情况的最佳做法是什么?