我继承了一个使用私有的构造函数,为什么我仍然能够从主函数访问它? [英] I Inherited a constructor using private, Why am I still be able to access it from main function?

查看:91
本文介绍了我继承了一个使用私有的构造函数,为什么我仍然能够从主函数访问它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

class base{
public:
    int i;
    base(){
    i=1;
    cout<<"Base Constructor";
    }
};

class derived: private base{
    public:
    derived(){
    i=2;
    cout<<"Derived constructor";
    }
};


int main(){ 
derived c;
return 0;
}

对于上面的代码,我得到的输出为
基础构造函数构造函数
即使我继承使用私有吗?

For the above code why am I getting the output as "Base ConstructorDerived Constructor" even though I inherited using private?

推荐答案


为什么我仍然可以从主函数访问它?

Why am I still be able to access it from main function?

您不是。

您的主函数访问 / code>的构造函数,它可以访问。

Your main function accesses derived's constructor, which it has access to.

base 的建构函式, 可存取。

And derived's constructor accesses base's constructor, which it has access to.

这篇关于我继承了一个使用私有的构造函数,为什么我仍然能够从主函数访问它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆