1
我有一個繼承基類(Base)的子類(Child),該基類以子類爲模板。子類也是一個類型的模板(可以是整數或任何...),我試圖在基類中訪問這種類型,我嘗試了很多東西沒有成功...這是我的想法可能是一個有效的解決方案更緊密,但並不編譯...訪問基類中的子模板參數(在子類上模板化)
template<typename ChildClass>
class Base
{
public:
typedef typename ChildClass::OtherType Type;
protected:
Type test;
};
template<typename TmplOtherType>
class Child
: public Base<Child<TmplOtherType> >
{
public:
typedef TmplOtherType OtherType;
};
int main()
{
Child<int> ci;
}
這裏是GCC告訴我:
test.cpp: In instantiation of ‘Base >’: test.cpp:14:7:
instantiated from ‘Child’ test.cpp:23:16: instantiated from here test.cpp:7:48: error: no type named ‘OtherType’ in ‘class Child’
這裏是一個可行的解決方案是等價的:
template<typename ChildClass, typename ChildType>
class Base
{
public:
typedef ChildType Type;
protected:
Type test;
};
template<typename TmplOtherType>
class Child
: public Base<Child<TmplOtherType>, TmplOtherType>
{
public:
};
但什麼煩惱我是重複的模板參數(轉發TmplOtherType爲Childtype)到基類...
你們認爲什麼?
那麼你是男人,它的工作原理就像魔術;)順便說一句,我不知道有關模板的模板參數,真棒!謝謝 ! – b3nj1