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. OOP作业总结一

    PS:建议用 Edge 查看此博客,Chrome 的话文章界面会有点窄,看起来可能会比较难受,我想改宽点但是不会改. 我会改了!改宽了一些,现在看起来舒服了很多,芜湖. 问题数据已修复,我们胜利辣! ...

  2. robot framework 导入资源

    创建资源后添加关键字 创建资源文件用于存放关键字,项目下的所有套件都可以引用. 1.创建资源 测试项目->new resource->输入资源名称->点击"确认" ...

  3. 访问kubernetes CRD的几种方式

    访问kubernetes CRD的几种方式 最近在使用代码操作VictoriaMetrics Operator的CRD资源的过程中,探究了集中访问CRD资源的方式.下面以VictoriaMetrics ...

  4. 学习JS的第三天

    一.逻辑分支(续) 1.三目运算符:条件运算符 a>b?c:d;表达式1?表达式2:表达式3; 根据表达式1执行的结果,来决定执行表达式2还是表达式3 表达式1结果是true执行表达式2,最终返 ...

  5. 体验webhooks

    一.webhooks是什么 webhooks是一种实现在web api跟web service之间的发布订阅的轻量级的模式:当服务中心某个事件发生的时候,就会向订阅者发送一个POST请求形式的通知,这 ...

  6. 大一C语言学习笔记(6)---自省篇--流程控制;break,continue,return间的异同;数组应用到循环语句中需要注意的问题;++i 和 i++的异同等。

    下面是傻瓜博主"曾经"犯过的错和一些心得:        ༼ つ ◕_◕ ༽つ 1.要想流程控制学好,一定要学会化繁为简,举栗子: 三目运算符 (略?略:略)---就是一个数字嘛, ...

  7. [luogu7340]Balance

    构造一个坐标系,共有$n$个黑点和百点,第$i$个黑点为$(p_{i},a_{i})$,第$i$个白点为$(-q_{i},-b_{i})$ 考虑第$i$个黑点和第$j$个白点连线的斜率,恰好就是$f( ...

  8. [loj3156]回家路线

    令$dp[i]$表示经过第$i$条边后的最小烦躁值,有$且dp[i]=\min_{y_{j}=x_{i}且q_{j}\le p_{i}}dp[j]+f(p_{i}-q_{j})$,其中$f(x)=Ax ...

  9. 类的访问权限和Object

    1.访问控制权限 1.1.访问控制权限都有哪些? 4个. private 私有 public 公开 protected 受保护 默认 1.2.以上的4个访问控制权限:控制的范围是什么? private ...

  10. negix启动不成功

    negix启动闪退,猜测可能端口占用,查看log发现 2020/11/30 11:38:40 [emerg] 15632#8688: CreateFile() "F:\项目工具\nginx- ...