且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么无法从派生类的实例访问受保护的成员?

更新时间:2023-02-15 15:02:22

是受保护的成员可以通过派生类访问,但是您正在main()函数中访问它,这是在层次结构之外。如果你在类B中声明一个方法并访问num,它会很好。

yes protected members are accessible by derived classes but you are accessing it in the main() function, which is outside the hierarchy. If you declare a method in the class B and access num it will be fine.