protected: C++ access control works on per-class basis, not on per-object basis
一个很简单的问题:
//为什么BASE::foo()中可以直接通过p访问val? 看本记录标题,这个问题困扰了很长一段时间,终于解决
class BASE
{
private:
int val;
public:void foo(BASE *p)
{
int w = p->val;
}
};
同学参加一场笔试,抛出个问题,丫凡和我讨论了下,丫凡在stackoverflow上找到了答案……
如下内容引述自:http://stackoverflow.com/questions/6986798/subtle-c-inheritance-error-with-protected-fields
==========================================================================
Below is a subtle example of accessing an instance's protected field x. B is a subclass of A so any variable of type B is also of type A. Why can B::foo() access b's x field, but not a's x field?
class A
{
protected:
int x;
}; class B : public A
{
protected:
A *a;
B *b;
public:
void foo()
{
int u = x; // OK : accessing inherited protected field x
int v = b->x; // OK : accessing b's protected field x
int w = a->x; // ERROR : accessing a's protected field x
}
};
Why can the B::foo() access the members of the contained class B pointer b?
The rule is:
In C++ access control works on per-class basis, not on per-object basis.
So an instance of class B will always have access to all the members of another instance of class B.
class A
{
protected:
int x;
}; class B : public A
{
public:
void foo()
{
int u = x; // OK : accessing inherited protected field
}
};
Since child is inheriting parent, child gets x. Hence you can access x directly in foo() method of child. This is the concept of protected variables. You can access protected variables of parent in child directly. Note : Here I am saying you can access x directly but not through A's object! Whats the difference ? Since, x is protected, you cannot access A's protected objects outside A. Doesnt matter where it is - If its main or Child . That's why you are not able to access in the following way.
Here comes an interesting concept. You can access a private variable of a class using its object with in the class!
class dummy
{
private :
int x;
public:
void foo()
{
dummy *d;
int y = d->x; // Even though x is private, still you can access x from object of d - But only with in this class. You cannot do the same outside the class.
}
};
protected: C++ access control works on per-class basis, not on per-object basis的更多相关文章
- In c++ access control works on per-class basis not on per-object basis.
#ifndef MYTIME_H #define MYTIME_H class MyTime { private: int m_hour; int m_minute; public: MyTime() ...
- Oracle Applications Multiple Organizations Access Control for Custom Code
档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...
- SELINUX、Security Access Control Strategy && Method And Technology Research - 安全访问控制策略及其方法技术研究
catalog . 引言 . 访问控制策略 . 访问控制方法.实现技术 . SELINUX 0. 引言 访问控制是网络安全防范和客户端安全防御的主要策略,它的主要任务是保证资源不被非法使用.保证网络/ ...
- [C++] OOP - Access Control and Class Scope
Access Control And Inheritance Protected Member Like private, protected members are unaccessible to ...
- 开源网络准入系统(open source Network Access Control system)
开源网络准入系统(open source Network Access Control system) http://blog.csdn.net/achejq/article/details/5108 ...
- [笔记] Access Control Lists (ACL) 学习笔记汇总
一直不太明白Windows的ACL是怎么回事,还是静下心来看一手的MSDN吧. [翻译] Access Control Lists [翻译] How Access Check Works Modify ...
- WebGoat系列实验Access Control Flaws
WebGoat系列实验Access Control Flaws Using an Access Control Matrix 在基于角色的访问控制策略中,每个角色都代表了一个访问权限的集合.一个用户可 ...
- Browser security standards via access control
A computing system is operable to contain a security module within an operating system. This securit ...
- A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS
1. INTRODUCTION The main goal of the National Computer Security Center is to encourage the widespr ...
随机推荐
- CSS网页布局中易犯的30个小错误
即使是CSS高手,也难免在书写CSS代码的时候出一些小错误,或者说,任何一种代码都是如此.小错误却往往造成大问题,浪费很多无辜的时间来调试和排错.查看下面这份CSS网页布局中易犯的10个小错误,努力的 ...
- JAVA-数据库之删除记录
相关资料:<21天学通Java Web开发> 删除记录 Statementdelete.jsp <%@ page language="java" contentT ...
- Eigen教程(8)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 原生缓存的接口:Map类 这篇将解释Eigen如何与原生raw C/C++ 数组混合 ...
- java基础篇---内存分析
Java的并发采用的是共享内存模型(而非消息传递模型),线程之间共享程序的公共状态,线程之间通过写-读内存中的公共状态来隐式进行通信.多个线程之间是不能直接传递数据交互的,它们之间的交互只能通过共享变 ...
- 9g10在nandflash扇区的分配地址
- 如何使用sendEmail发送邮件
sendEmail是一个轻量级,命令行的SMTP邮件客户端.如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php.bashperl和web站 ...
- HTML select 选中触发事件
$(function () { $("#cityidchange").change(function (data) { var cityid = $("#cityidch ...
- im ui框架调研,对比
1, https://github.com/jpush/aurora-imui 极光的 支持 Android/iOS/RN 提供了消息列表.输入视图 功能不多, 2,https://github.co ...
- js json处理 双引号
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象 JSON字符串: var str1 = '{ "name": "cxh", ...
- Android Studio安装Kotlin插件
1 Kotlin语言介绍 Kotlin 是 JetBrains 在 2010 年推出的基于 JVM 的新编程语言,是一种新的静态类型编程语言.开发者称,设计它的目的是避免 Java 语言编程中的一些难 ...