具有模板和可见性的C 继承

c++ Inheritance with templates and visibility

本文关键字:继承 可见性      更新时间:2023-10-16

我不明白ATT与模板的所有继承。

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()"放在公共场合中时,所有人都起作用吗?我不能为我的班级提供"受保护的"功能?为什么?

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

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

有关详细的讨论,请参阅为什么受保护的构造函数提出错误此代码?