为什么在保护模式下继承升级不起作用

Why in protected mode inheritance upcasting is not working

本文关键字:不起作用 继承 保护 模式 为什么      更新时间:2023-10-16

当我在受保护模式下继承类时,Derived* to Base*失败的原因

class Base { };
class Derived : protected Base { };
int main()
{
Base* b = new Derived(); // compile error
}

对基类的访问与对任何成员的访问相同。从外面看,根本禁止进入。只有特权代码(可以访问其他受保护甚至私有部分的代码(才能以这种方式访问基类。