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 ...
随机推荐
- javascript 常用的正则表达式验证表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux记录-Linux 企业运维人员最常用 150 个命令
命令 功能说明 线上查询及帮助命令 (2 个) man 查看命令帮助,命令的词典,更复杂的还有 info,但不常用. help 查看 Linux 内置命令的帮助,比如 cd 命令. 文件和目录操作命令 ...
- Hbase记录-HBase基本操作(二)
HBase Exists 可以使用exists命令验证表的存在.下面的示例演示了如何使用这个命令. hbase(main):024:0> exists 'emp' Table emp doe ...
- jdk1.6 反射性能对比【原】
ReflectPerformance.java package aaa.bbb.ccc; import java.lang.reflect.Method; public class ReflectPe ...
- JavaScript 数字转汉字+element时间选择器快速选择
window.CN = { : '一', : '二', : '三', : '四', : '五', : '六', : '七', : '八', : '九', : '零' } window.LEVEL = ...
- centos redis集群搭建
说明: 10.0.0.111部署6500,6501,6502三个主节点 10.0.0.222部署6500,6501,6502三个备份节点 1.安装redis:略 2.配置内核参数 # 配置 vm.ov ...
- 三、u-boot 的配置-mkconfig 脚本
3.1 mkconfig 脚本 100ask24x0_config : unconfig @$(MKCONFIG) $(@:_config=) arm arm920t 100ask24x0 NULL ...
- 有关Linux的.a、.so和.o文件(转)【原文章有些错误,自己已更改】
gcc 生成 .a静态库和 .so动态库 我们通常把一些公用函数制作成函数库,供其它程序使用.函数库分为静态库和动态库两种.静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库.动态库 ...
- IMU 预积分推导
给 StereoDSO 加 IMU,想直接用 OKVIS 的代码,但是有点看不懂.知乎上郑帆写的文章<四元数矩阵与 so(3) 左右雅可比>提到 OKVIS 的预积分是使用四元数,而预积分 ...
- 超详细的Web前端开发规范文档
规范目的为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进行前台页面开发. 本文档如有不对或者不合 ...