C++笔记:派生类到基类转换到可访问性
要确定到基类的转换是否可访问,可以考虑基类的public成员是否可访问,如果可以,转换是可以访问的,否则,转换是不可访问的。
- 如果是public继承,则用户代码和后代类都可以使用派生类到基类的转换。
- 如果类是使用private或protected继承派生的,则用户代码不能将派生类型对象转换为基类对象。
- 如果是private继承,则从private继承类派生的类不能转换为基类。
- 如果是protected继承,则后续派生类的成员可以转换为基类类型。
无论是什么派生访问标号,派生类本身都可以访问基类的public成员,因此,派生类本身的成员和友元总是可以访问派生类到基类的转换。
Like an inherited member function, the conversion from derived to base may or may not be accessible. Whether the conversion is accessible depends on the access label specified on the derived class' derivation.
If the inheritance is public, then both user code and member functions of subsequently derived classes may use the derived-to-base conversion. If a class is derived using private or protected inheritance, then user code may not convert an object of derived type to a base type object. If the inheritance is private, then classes derived from the