C++笔记:派生类到基类转换到可访问性
要确定到基类的转换是否可访问,可以考虑基类的public成员是否可访问,如果可以,转换是可以访问的,否则,转换是不可访问的。
- 如果是public继承,则用户代码和后代类都可以使用派生类到基类的转换。
- 如果类是使用private或protected继承派生的,则用户代码不能将派生类型对象转换为基类对象。
- 如果是private继承,则从private继承类派生的类不能转换为基类。
- 如果是protected继承,则后续派生类的成员可以转换为基类类型。
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

本文介绍了C++中派生类到基类转换的可访问性规则,包括public、protected和private继承的情况。public继承时,用户代码和后续派生类均可进行转换;private或protected继承时,转换受限。文中通过测试代码和解析详细阐述了这些规则,并提供了判断转换是否可访问的提示。
最低0.47元/天 解锁文章
436

被折叠的 条评论
为什么被折叠?



