c ++具有模板和可见性的继承

时间:2016-12-13 20:32:57

标签: c++ templates inheritance visibility

我不明白所有继承模板..

template <typename T> 
class Mere 
{ 
protected:
    Mere();
}; 

class Fille2 : public Mere<int>
{ 
protected:
    Fille2(){
        Mere();
    }
}; 


int main(int argc, char** argv) {

    return 0;
}

为什么会出现此错误?

main.cpp:22:5: error: 'Mere<T>::Mere() [with T = int]' is protected
Mere();

当我把“Mere()”公之于众时,一切正常吗?我不能为我的班级“仅仅”提供“保护”功能吗?为什么呢?

1 个答案:

答案 0 :(得分:3)

是的,您可以调用基类构造函数,即使它是protected。这是正确的语法:

class Fille2 : public Mere<int>
{ 
protected:
    Fille2(): Mere() {
    }
}; 

有关详细讨论,请参阅Why is protected constructor raising an error this this code?