c++ primer 学习杂记2【派生类到基类转换的可访问性】
参考:
http://blog.csdn.net/rehongchen/article/details/7930853
http://blog.csdn.net/ming_road/article/details/6953687
http://blog.csdn.net/roden/article/details/5413371
中文版:p489 。对应英文版内容:
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 privately inherited class may not convert to the base class. If the inheritance is protected, then the members of subsequently derived classes may convert to the base type.
Regardless of the derivation access label, a public member of the base class is accessible to the derived class itself. Therefore, the derived-to-base conversion is always accessible to the members and friends of the derived class itself.
Tips:To determine whether the conversion to base is accessible, consider whether a public member of the base class would be accessible. If so, the conversion is accessible; otherwise, it is not.
提示:要确定到基类的转换是否可访问,可以考虑基类的public成员是否可访问,如果可以,转换是可以访问的,否则,转换是不可访问的。
首先要明白几个概念:用户代码(user code), 后代类(subsequently derived classes),派生类(derived class)
用户代码,指的是除友元函数,成员函数之外的代码。
后代类,不仅仅指第一级派生类,还包括间接派生自基类的后续的派生类。
派生类,这里专指直接继承类。
理解了上面的概念之后,通过代码来说明c++ primer中说到的4点。
class A
{
}; class B:public A
{
public:
void fun(B&obj)
{
A obj1 = (A)obj;
}
}; class C:protected A
{
public:
void fun(C&obj)
{
A obj1 = (A)obj;
}
}; class D:private A
{
public:
void fun(D&obj)
{
A obj1 = (A)obj;
}
}; class E:public B
{
public:
void fun(B&obj)
{
A obj1 = (A)obj;
}
}; class F:public C
{
public:
void fun(C&obj)
{
A obj1 = (A)obj;
}
}; //从private继承类派生的类不能转换为基类。
class H:public D
{
public:
void fun(D&obj)
{
A obj1 = (A)obj; //error C2247: “A”不可访问,因为“D”使用“private”从“A”继承
//error C2243: “类型转换”: 从“D *”到“const A &”的转换存在,但无法访问
}
};
用户代码中访问的内容:
void inherite_test()
{
A *pb, *pc, *pe, *pd, *pf, *ph; pb = new B; //public
pc = new C; //protected error C2243: “类型转换”: 从“C *”到“A *”的转换存在,但无法访问
pd = new D; //private error C2243: “类型转换”: 从“D *”到“A *”的转换存在,但无法访问
pe = new E; //public + public
pf = new F; //protected + public error C2243: “类型转换”: 从“F *”到“A *”的转换存在,但无法访问
ph = new H; //private + public error C2243: “类型转换”: 从“H *”到“A *”的转换存在,但无法访问
}
解析:
其中类B,C,D分别通过public,protected,private直接继承自A; 类E,F,H则分别public继承自B,C,D类。
各类的成员函数fun中进行派生类到基类的转换操作。
1、pb = new B; B::fun,E::fun函数说明:
如果是public继承,则用户代码和后代类都可以使用派生类到基类的转换。
If the inheritance is public, then both user code and member functions of subsequently derived classes may use the derived-to-base conversion
2、inherite_test中C,D,F,H类转换的失败都说明了:
如果类是使用private或protected继承派生的,则用户代码不能将派生类型对象转换为基类对象。
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.
3、H类的fun函数发生的错误显示:
从private继承类派生的类不能转换为基类。
If the inheritance is private, then classes derived from the privately inherited class may not convert to the base class.
4、C::fun函数说明:
如果是protected继承,则后续派生类的成员可以转换为基类类型。
If the inheritance is protected, then the members of subsequently derived classes may convert to the base type.
有一点需要说明:对于多级派生的,要多个访问标号综合起来看可访问性。 有个简单的方法,见上面的tips.
例如,C 类protected继承自A,那么A中的public成员在C中变成了protected, F类public继承自C,这样在F中A的public成员fun函数为protected,是可见的。
所以F::fun中派生类到基类转换正确。
但是在用户代码中,是不能访问在C,F中变成了protected的A的public成员的,因此C,F对象转换为类A的对象出错。
=====>感觉fun函数定义的有点不合适,挺奇怪的,但是编译是没问题的,不知道现实生产中有没有这么用的??? (待定)
c++ primer 学习杂记2【派生类到基类转换的可访问性】的更多相关文章
- c++ primer 学习杂记1
读到p483 公有,私有,受保护的继承. 1.关于基类成员在派生类中的访问级别: 1) 无论何种继承方式, 派生类都无法访问基类中的private成员. 2) 派生类可以限制,而不能放松对所继承成员的 ...
- c++ 派生类向基类转换的可访问性
对于c++面向对象一直很疑惑,这次决定下功夫把它弄明白 一.派生类和基类之间的类型转换 首先理解,派生类含有基类的所有成分,只不过有些就算在派生类的成员函数也不能访问而已. (1)派生类和基类的自动转 ...
- 从零开始学C++之继承(二):继承与构造函数、派生类到基类的转换
一.不能自动继承的成员函数 构造函数 析构函数 =运算符 二.继承与构造函数 基类的构造函数不被继承,派生类中需要声明自己的构造函数. 声明构造函数时,只需要对本类中新增成员进行初始化,对继承来的基类 ...
- C++ 派生类到基类转换的可访问性
今天看c++ primer关于派生类到基类转换的可访问性,看的很晕,看了下面的文章恍然大悟: http://www.2cto.com/kf/201403/283389.html C++ primer第 ...
- C#中派生类调用基类构造函数用法分析
这里的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.当基类中没有自己编写构造函数时,派生类默认的调用基类的默认构造函数例如: ? 1 2 3 4 5 6 7 8 9 10 11 ...
- 转 关于C#中派生类调用基类构造函数的理解
关于C#中派生类调用基类构造函数的理解 .c#class 本文中的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1. 当基类中没有自己编写构造函数时,派生类默认的调用 ...
- c++中派生类对基类成员的三种访问规则(转)
C++中派生类对基类成员的访问形式主要有以下两种:1.内部访问:由派生类中新增成员对基类继承来的成员的访问.2.对象访问:在派生类外部,通过派生类的对象对从基类继承来的成员的访问.今天给大家介绍在3中 ...
- c++——派生类和基类转换(类型兼容性原则)
基类也叫父类,派生类也叫子类. 类之间的继承关系继承关系是类之间的父子关系. 继承关系的特点如下:A. 子类拥有父类的所有属性和行为B. 子类也是一种特殊的父类C. 子类对象可以当父类对象使用D. 子 ...
- qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)
原博主博客地址:http://blog.csdn.net/qq21497936本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516 ...
随机推荐
- centos6.5环境下安装zk
第一步:先下载安装包,解压. 第二步:进去根目录,创建data文件夹 mkdir data 第三步:进去conf文件夹,修改 zoo_sample.cfg 的名字 mv zoo_sam ...
- windows Zookeeper本地服务化
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...
- Servlet总结(一)
一.Servlet了解 1.Servlet全称Java Servlet,是用java编写的独立于平台和协议的服务器端应用程序,运行于服务器,采用请求-响应模式提供Web服务 2.Servlet实现过程 ...
- WF控制台工作流(2)
using System; using System.Linq; using System.Activities; using System.Activities.Statements; namesp ...
- C# 面向对象的new关键字的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- ==========2014-04-24=========winform树控件勾选方法 和获取所有选中的
http://bbs.bccn.net/thread-197567-1-1.html /// <summary> /// 已选中或取消选中树节点上的复选框时 /// </summar ...
- JavaScript之子类构建工具
(function(){ var initializing = false; var superPattern = /xyz/.test(function(){ xyz; }) ? /\b_super ...
- Error: Cannot find module PhantomJS
node install.js Considering PhantomJS found at /usr/local/bin/phantomjs Looks like an `npm install - ...
- ubuntu16.04+anaconda的安装+解决conda不可用(配置路径)+卸载
首先一点,之前我一直自己安装python,然后直接在python环境下再安装第三方库,但自从另一台电脑重装系统之后,我当时在没有python的情况下直接安装的anaconda,觉得她超级好用(所以如果 ...
- Broadcast的类型
两种发送方法 1.无序广播 对于多个接收者来说是完全异步的,通常每个接收者都无需等待即可以接收到广播,接收者相互之间不会有影响.对于这种广播,接收者无法终止广播,即无法阻止其他接收者的 接收动作. 消 ...