【发布时间】:2014-06-19 18:43:15
【问题描述】:
我希望从通过 C::A 和 B::A 继承的子类 D 调用基类 A 的方法。
template <class PType>
class A
{
public:
template <class ChildClass>
void Func( void )
{
std::cout << "Called A<PType>::Func<ChildClass>" << std::endl;
}
};
template <class PType>
class B : public A<PType>
{
};
template <class PType>
class C : public A<PType>
{
};
class D : public B<int>, public C<int>
{
public:
D()
{
static_cast<B<int>*>( this )->A<int>::Func<D>();
static_cast<C<int>*>( this )->A<int>::Func<D>();
}
};
这按预期工作,D 在初始化时使用子类的模板参数调用 B::A::Func 和 C::A::Func。但是,当 D 是模板类时,这似乎不起作用。
template <class PType>
class D2 : public B<PType>, public C<PType>
{
public:
D2()
{
//expected primary-expression before ‘>’ token
//expected primary-expression before ‘)’ token
static_cast<B<PType>*>( this )->A<PType>::Func< D2<PType> >();
static_cast<C<PType>*>( this )->A<PType>::Func< D2<PType> >();
}
};
问题似乎是 Func 的模板参数 D2,但除此之外无法解决。
【问题讨论】:
-
试试
static_cast<B<PType>*>(this)->template A<PType>::Func< D2<PType> >();
标签: c++ templates inheritance casting