考慮以下類:問題訪問基地成員在派生構造
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並且我已經將它初始化爲0
的BarBC
構造函數有關?
@George的參數附加BarBC構造 - 謝謝。我試圖默認初始化基類構造函數中的所有常量數據成員(以避免編譯錯誤),然後在派生構造函數中設置派生類特定成員(同時仍將成員留在基類中)。不是任何措施最好的階級結構。但我沒有意識到初始化列表有這樣的限制。 – LeopardSkinPillBoxHat 2010-06-01 06:28:14