This question already has answers here:
Why doesn't a derived template class have access to a base template class' identifiers?

(4 个回答)


5年前关闭。




以下代码
template<int c>
struct Base
{
    static const int a = c + 5;
};

template<int c>
struct Derived : Base<c>
{
    static const int b = a + 5;
};

... 无法编译,因为 a was not declared in this scope 。明确指定 Base<c>::a 有效,但从逻辑上讲,这不是必需的,因为我们是从 Base<c> 派生的。这是预期的行为(以及为什么)还是我错过了什么?

最佳答案

template<int c>
struct Derived : Base<c>
{
    static const int b = Base<c>::a + 5;
};

它真的取决于编译器,但是 gcc 需要它,当涉及到模板类时它很愚蠢

关于c++ - 模板类 : static members not inherited,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31147233/

10-11 22:09
查看更多