What happens when more restrictive access is given to a derived class method in C++?
We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods.
For example the following program compiles fine.
1 #include<iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 virtual int fun(int i)
8 {
9 }
10 };
11
12 class Derived: public Base
13 {
14 private:
15 int fun(int x)
16 {
17 }
18 };
19
20 int main()
21 {
22 }
In the above program, if we change main() to following, will get compiler error becuase fun() is private in derived class.
1 int main()
2 {
3 Derived d;
4 d.fun(1);
5 return 0;
6 }
What about the below program?
1 #include<iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 virtual void fun(int i)
8 {
9 cout << "Base::fun(int i) called";
10 }
11 };
12
13 class Derived: public Base
14 {
15 private:
16 void fun(int x)
17 {
18 cout << "Derived::fun(int x) called";
19 }
20 };
21
22 int main()
23 {
24 Base *ptr = new Derived;
25 ptr->fun(10);
26 return 0;
27 }
Output:
Derived::fun(int x) called
In the above program, private function “Derived::fun(int )” is being called through a base class pointer, the program works fine because fun() is public in base class. Access specifiers are checked at compile time and fun() is public in base class. At run time, only the function corresponding to the pointed object is called and access specifier is not checked. So a private function of derived class is being called through a pointer of base class.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 20:42:53
What happens when more restrictive access is given to a derived class method in C++?的更多相关文章
- The third column indicates whether subclasses of the class declared outside this package have access to the member.;
总结1.modifier 改性剂 修饰符 修饰语 调节器access modifier 访问修饰符 存取权限 存取修饰词 存取修饰字官网“can”Access level modifiers dete ...
- A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS
1. INTRODUCTION The main goal of the National Computer Security Center is to encourage the widespr ...
- Method and system for implementing mandatory file access control in native discretionary access control environments
A method is provided for implementing a mandatory access control model in operating systems which na ...
- [C++] OOP - Access Control and Class Scope
Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...
- Public Private Protect Inheritance and access specifiers
In the previous lessons on inheritance, we've been making all of our data members public in order to ...
- swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明
关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...
- System Error Codes
很明显,以下的文字来自微软MSDN 链接http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx M ...
- Ubuntu 12.04 安装MySQL
本文地址:http://www.cnblogs.com/yhLinux/p/4012689.html 本文适合新手入门. 本文是对 Ubuntu 12.04 环境下安装 MySQL 的记录,通过这两天 ...
- SNMP 原理与实战详解
原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
随机推荐
- 【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...
- 热门剧本杀与 SaaS 的不解之缘
近年来,"剧本杀"这种以剧本为核心,玩家分别扮演不同角色推理案情找出真凶的娱乐项目在年轻人的范围内迅速传开,已悄然形成了一个市场规模超百亿的新兴产业,吸引了大量淘金者.而在互联网时 ...
- Vue.js教程 2.体验Vue
Vue.js教程 2.体验Vue <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- [python]RobotFramework自定义库实现UI自动化
1.安装教程 环境搭建不多说,网上资料一大堆,可参考https://www.cnblogs.com/puresoul/p/3854963.html,写的比较详细,值得推荐.目前python3是不支持r ...
- 大一C语言学习笔记(2)---快捷键篇
大家好,博主呢,是一位刚刚步入大一的软件工程专业的大学生,之所以写博客,是想要与同样刚刚接触程序员一行的朋友们一起讨论,进步,在这里记录我的一些学习笔记及心得,希望通过这些点点滴滴的努力,可以让我们离 ...
- Java核心技术--Java程序设计
Java术语 术语名 缩写 解释 Java Development Kit(Java开发工具包) JDK 编写Java程序的程序员使用的软件 Java Runtime Environment(Java ...
- 微软商店打不开的教程(错误代码0x80131500)
1 打开win+R 输入`inetcpl.cpl` 2 点击高级 3 勾选上`使用TLS 1.2`或者点击还原默认设置就可以啦 4 然后就可以打开啦
- [atAGC052F]Tree Vertices XOR
结论 注意到如果$x$周围有偶数个1,对$x$操作显然不会改变$a_{x}$,因此不妨强制操作的点周围要有奇数个1,不难发现此时恰好会改变该点,即令$a_{x}=a_{x}\oplus 1$ 称$\{ ...
- [loj3341]时代的眼泪
题意即求在区间$[l,r]$中且权值在$[x,y]$中的逆序对个数 考虑分块,逆序对个数包含4部分: 1.左/右块外内部,预处理出$i$到其所在块的块首/尾,小于/小于等于$j$(需要对$j$离散)的 ...
- 直接插入100w数据报错
### Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (77,600 ...