一个很简单的问题:

//为什么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的更多相关文章

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

  2. Oracle Applications Multiple Organizations Access Control for Custom Code

    档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code ...

  3. SELINUX、Security Access Control Strategy && Method And Technology Research - 安全访问控制策略及其方法技术研究

    catalog . 引言 . 访问控制策略 . 访问控制方法.实现技术 . SELINUX 0. 引言 访问控制是网络安全防范和客户端安全防御的主要策略,它的主要任务是保证资源不被非法使用.保证网络/ ...

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

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

  5. 开源网络准入系统(open source Network Access Control system)

    开源网络准入系统(open source Network Access Control system) http://blog.csdn.net/achejq/article/details/5108 ...

  6. [笔记] Access Control Lists (ACL) 学习笔记汇总

    一直不太明白Windows的ACL是怎么回事,还是静下心来看一手的MSDN吧. [翻译] Access Control Lists [翻译] How Access Check Works Modify ...

  7. WebGoat系列实验Access Control Flaws

    WebGoat系列实验Access Control Flaws Using an Access Control Matrix 在基于角色的访问控制策略中,每个角色都代表了一个访问权限的集合.一个用户可 ...

  8. Browser security standards via access control

    A computing system is operable to contain a security module within an operating system. This securit ...

  9. A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS

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

随机推荐

  1. java框架篇---Struts入门

    首先理解Struts与MVC的关系 在传统的MVC模式中所有的请求都要先交给Servlet处理,之后由Servlet调用JavaBean,并将结果交给JSP中进行显示.结构图如下 Struts是Apa ...

  2. Ubuntu 14.04 编译 Android 4.2.2 for Tiny4412

    . . . . . 在学校里是用 Redhat 6.4 编译的 Android 4.2.2 很顺利,把源码包拷贝到笔记本上的 Ubuntu 14.04 上再编译遭遇了各种坑,所以便有了这篇博客记录解决 ...

  3. C语言 · 学做菜

    算法训练 学做菜   时间限制:1.0s   内存限制:256.0MB      问题描述 涛涛立志要做新好青年,他最近在学做菜.由于技术还很生疏,他只会用鸡蛋,西红柿,鸡丁,辣酱这四种原料来做菜,我 ...

  4. TCP和UDPsocket中SO_SNDBUF和SO_RCVBUF_转

    1.Background Winsock kernel buffer To optimize performance at the application layer, Winsock copies ...

  5. 25个最常用的iptables策略

    1.清空存在的策略当你开始创建新的策略,你可能想清除所有的默认策略,和存在的策略,可以这么做:iptables -F  或者iptables --flush2,设置默认策略默认链策略是ACCEPT,改 ...

  6. maven 从私仓库下载jar包

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  7. 解决EasyUi中的DataGrid删除一条记录后,被删除的数据仍处于被选中状态问题

    项目中遇到这么一个问题,在easyui的datagrid中,删除一条记录成功,重新加载datagrid后,去点编辑操作,仍可把之前删除掉的那条记录进行编辑操作,正常情况应该是,删除后再去点击“编辑”, ...

  8. Netty实践

    Netty是JBOSS针对网络开发的一套应用框架,它也是在NIO的基础上发展起来的.netty基于异步的事件驱动,具有高性能.高扩展性等特性,它提供了统一的底层协议接口,使得开发者从底层的网络协议(比 ...

  9. js在IE与firefox的差别。。。

    1.firefox不能对innerText支持.firefox支持innerHTML但却不支持innerText,它支持textContent来实现innerText,不过默认把多余的空格也保留了.如 ...

  10. Sword protobuf学习一

    protobuf简介 Protocol Buffers,是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 它不依赖于语言和平台并且可扩展性 ...