【问题标题】:c++ Inheritance with templates and visibility带有模板和可见性的 c++ 继承
【发布时间】:2017-04-29 01:02:40
【问题描述】:

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

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()”公之于众时,一切正常吗?我的班级“Mere”不能有“受保护”的功能吗?为什么?

【问题讨论】:

  • 这不是你在 c++ 中调用父类的构造函数的方式...请参阅here 了解如何完成

标签: c++ templates inheritance visibility


【解决方案1】:

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

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

详细讨论见Why is protected constructor raising an error this this code?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 2010-12-06
    • 2012-10-14
    • 2013-01-02
    • 2011-03-03
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多