具有模板和可见性的 c++ 继承
c++ Inheritance with templates and 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()" 放入 public 时一切正常吗?我的 class "Mere" 不能有 "protected" 功能?为什么?
是的,你可以调用基础class构造函数,即使它是protected
。这是正确的语法:
class Fille2 : public Mere<int>
{
protected:
Fille2(): Mere() {
}
};
详细讨论见Why is protected constructor raising an error this this code?
我不明白模板的所有继承..
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()" 放入 public 时一切正常吗?我的 class "Mere" 不能有 "protected" 功能?为什么?
是的,你可以调用基础class构造函数,即使它是protected
。这是正确的语法:
class Fille2 : public Mere<int>
{
protected:
Fille2(): Mere() {
}
};
详细讨论见Why is protected constructor raising an error this this code?