gpt4 book ai didi

c++ - 我继承了一个使用私有(private)的构造函数,为什么我仍然可以从主函数访问它?

转载 作者:行者123 更新时间:2023-11-30 02:00:32 24 4
gpt4 key购买 nike

#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;
}

对于上面的代码,为什么我得到的输出是“基础构造函数派生构造函数”即使我继承使用私有(private)?

最佳答案

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

你不是。

您的主函数访问它有权访问的 derived 的构造函数。

并且 derived 的构造函数访问 base 的构造函数,it 可以访问该构造函数。

关于c++ - 我继承了一个使用私有(private)的构造函数,为什么我仍然可以从主函数访问它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com