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++?的更多相关文章

  1. 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 ...

  2. A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS

    1. INTRODUCTION   The main goal of the National Computer Security Center is to encourage the widespr ...

  3. 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 ...

  4. [C++] OOP - Access Control and Class Scope

    Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...

  5. 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 ...

  6. swift 中关于open ,public ,fileprivate,private ,internal,修饰的说明

    关于 swift 中的open ,public ,fileprivate,private, internal的区别 以下按照修饰关键字的访问约束范围 从约束的限定范围大到小的排序进行说明 open,p ...

  7. System Error Codes

    很明显,以下的文字来自微软MSDN 链接http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx M ...

  8. Ubuntu 12.04 安装MySQL

    本文地址:http://www.cnblogs.com/yhLinux/p/4012689.html 本文适合新手入门. 本文是对 Ubuntu 12.04 环境下安装 MySQL 的记录,通过这两天 ...

  9. SNMP 原理与实战详解

    原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...

随机推荐

  1. Java实体映射工具MapStruct使用详解

    1.序 通常在后端开发中经常不直接返回实体Entity类,经过处理转换返回前端,前端提交过来的对象也需要经过转换Entity实体才做存储:通常使用的BeanUtils.copyProperties方法 ...

  2. 三(二)、AOP配置

    一.AOP的配置(注解) 步骤一.导入jar包: 处理那5个jar包之外,还需要导入: aopalliance aspectjweaver spring-aop spring-aspects 步骤二. ...

  3. Swift进阶-内存管理

    本文的主要目的是探索 RefCount 的内存结构及强/弱引用计数管理 Swift 中也是采用 ARC 编译器自动内存管理机制. Swift 对象的内存结构是 HeapObject, 有两个属性 Me ...

  4. Java操作MongoDB之mongodb-driver(一)

    1. mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动. (1)通过maven仓库导入:https://mvnrepository.com/ar ...

  5. Python基础入门(1)- Python环境搭建与基础语法

    Python编程环境搭建 Python环境搭建 官网下载:https://www.python.org/ python --version PyCharm下载安装 安装 官网下载:https://ww ...

  6. jmeter压测IP欺骗绕过服务端限流

    1.环境声明 jmeter3.0 后端为内网环境 2.检查内网闲置的ip 工具地址,无需复杂安装,解压点击就可以用啦~~ https://pan.baidu.com/s/1Yzs1vezfFMoy-m ...

  7. Linux可执行文件格式-ELF结构详解

    表1. ELF文件类型分类 ELF文件类型 说明 实例 Relocatable File 可重定位文件 未链接之前的ELF文件,可用于链接可执行文件或静态链接库 Linux下的".o&quo ...

  8. 探究 Go 源码中 panic & recover 有哪些坑?

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.luozhiyun.com/archives/627 本文使用的go的源码1.17.3 前言 写这一篇文章的原因是 ...

  9. I.MX启动方式和头部

    1. 启动方式 2. 头部信息 编译好的bin文件烧写到SD卡中,需要加一些头部文件,才可以执行. Image vector table,简称 IVT,IVT 里面包含了一系列的地址信息,这些地址信息 ...

  10. Electron快速入门之事件

    const { app, BrowserWindow } = require('electron') function createWindow () {   const win = new Brow ...