为什么在保护模式下继承向上转换不起作用

时间:2018-11-04 06:22:39

标签: c++

当我以保护模式继承类时,Derived* to Base*为何失败

class Base { };

class Derived : protected Base { };

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

1 个答案:

答案 0 :(得分:0)

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