【发布时间】:2010-06-01 06:08:17
【问题描述】:
给定以下类:
class Foo
{
struct BarBC
{
protected:
BarBC(uint32_t aKey)
: mKey(aKey)
mOtherKey(0)
public:
const uint32_t mKey;
const uint32_t mOtherKey;
};
struct Bar : public BarBC
{
Bar(uint32_t aKey, uint32_t aOtherKey)
: BarBC(aKey),
mOtherKey(aOtherKey) // Compile error here
};
};
我在指示的地方遇到编译错误:
error: class `Foo::Bar' does not have any field named `mOtherKey'.
谁能解释一下?我怀疑这是一个语法问题,因为我的 Bar 类是在 Foo 类中定义的,但似乎找不到解决方法。
这是简单的公共继承,所以mOtherKey 应该可以从Bar 构造函数中访问。对吧?
还是与 mOtherKey 是 const 并且我已经在 BarBC 构造函数中将其初始化为 0 的事实有关?
【问题讨论】:
标签: c++ inheritance constructor