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 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
随机推荐
- AppScan 10安装使用
一.简介 AppScan是IBM的一款web安全扫描工具,具有利用爬虫技术进行网站安全渗透测试的能力,能够根据网站入口自动摸取网页链接进行安全扫描,提供了扫描.报告和修复建议等功能. appscan有 ...
- 《手把手教你》系列技巧篇(三十八)-java+ selenium自动化测试-日历时间控件-下篇(详解教程)
1.简介 理想很丰满现实很骨感,在应用selenium实现web自动化时,经常会遇到处理日期控件点击问题,手工很简单,可以一个个点击日期控件选择需要的日期,但自动化执行过程中,完全复制手工这样的操作就 ...
- python连接集群mongodb,封装增删改查
1.下载pymongo pip install pymongo 2.直接上代码 [ini配置文件] 封装读ini省略~~ [db.py] class Database(): def __init__( ...
- gcc: fatal error: limits.h: No such file or directory on macos
重装gcc brew install gcc 软链接链到新的gcc和g++ https://stackoverflow.com/questions/56280122/gcc-fatal-error-l ...
- Swift学习笔记(一)
1.Constants and Variables(常量和变量) let定义常量,var定义变量. [Note] If a stored value in your code won't change ...
- 配置Google支付相关参数(client_id, client_secret, refresh_token)
1. 登陆Google开发者账号,点击左边API权限 Google控制台 创建新项目 转到 Google Play 管理中心的 API 权限页面. 接受<服务条款>. 点击创建新项目. 系 ...
- php 数组(2)
数组排序算法 冒泡排序,是一种计算机科学领域的较简单的排序算法.它重复地访问要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们减缓过来.走访数列的工作室重复的进行直到没有再需要交换,也就是说该 ...
- 【Sass/SCSS】预加载器中的“轩辕剑”
[Sass/SCSS]预加载器中的"轩辕剑" 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 随着前端 ...
- [bzoj1416]神奇的口袋
容易发现操作任意次并不会改变每一个点的概率(因为每一个点加d的概率相同,期望与原数成比例),然后直接输出即可(要用高精度) 1 #include<bits/stdc++.h> 2 usin ...
- 微信小程序的优点(水一篇)
- 快速的加载 - 更强大的能力 - 原生的体验 - 易用且安全的微信数据开放 - 高效和简单的开发 摘自微信官方文档 https://developers.weixin.qq.com/minipro ...