C++笔记:派生类到基类转换到可访问性

本文介绍了C++中派生类到基类转换的可访问性规则,包括public、protected和private继承的情况。public继承时,用户代码和后续派生类均可进行转换;private或protected继承时,转换受限。文中通过测试代码和解析详细阐述了这些规则,并提供了判断转换是否可访问的提示。
摘要由CSDN通过智能技术生成

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 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值