下面是一個簡單的類層次結構,其中混合了模板化和非模板化類。嵌入在這個例子的評論是我想解決的編譯錯誤。混合模板/非模板繼承分類和成員繼承
我在這裏閱讀了一些其他相關的線索,並嘗試了幾種形式的「使用」關鍵字無濟於事。
我在這些試驗中瞭解到: 父:: parent_ivar_ = 99; 是可行的語法,但不知道如何告訴編譯器關於我想在Child類中設置/使用的繼承的ivar grandparent_ivar_。
在此先感謝!
class GrandParent {
public:
int grandparent_ivar_;
};
template <typename T>
class Parent : public GrandParent {
public:
int parent_ivar_;
};
template <typename T>
class Child : public Parent<T> {
public:
Child() {
// The following stmt produces this compiler error using ubuntu g++:
// templateinheritance.cpp: In constructor ‘Child<T>::Child()’:
// templateinheritance.cpp:20:5: error: ‘grandparent_ivar_’ was
// not declared in this scope
// grandparent_ivar_ = 100;
//
grandparent_ivar_ = 100;
}
};
int main(int argc, char *argv[]) {
Child<int> c;
}
謝謝你的好信息山姆。是的,我錯過了這個例子中的公共繼承。我認爲這與問題沒有密切關係。我會研究這個 - > foo解決方案。隨着一些進一步的調查「使用GrandParent :: grandparent_ivar;」放置在子類聲明中也起作用。乾杯。 – user501138