【发布时间】:2015-03-10 10:59:08
【问题描述】:
我有以下情况,我声明了一个超类的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
This answer 解释了为什么不能在 子类的 构造函数的 list initiliazers 中初始化超类的成员变量。我想知道这种情况下的最佳做法是什么?
【问题讨论】:
标签: c++ polymorphism list-initialization