一个很简单的问题:

//为什么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. word中的域代码

    说明(2017-5-23 13:33:11): 1. Shift+F9显示域代码 2. Alt+F9显示全部域代码 3. Ctrl+F9添加一个域代码(一对大括号) 4. Ctrl+Shift+F9取 ...

  2. Eigen教程(2)

    整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html Matrix类 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vect ...

  3. 引用Interop.SQLDMO.dll后的注意事项。

    SQLDMO.dll是个好东西,ASP.NET利用它可以实现在线备份.还原数据库等各种功能.近日有客户要求为其在后台添加一个管理数据库的功能.于是就出现了这篇文章.     由于客户的数据库和WEB服 ...

  4. 【Unity】使用Resources类管理资源

    最近参考了各位大神的资源,初步学习了Unity的资源管理模式,包括在编辑器管理(使用AssetDatabase)和在运行时管理(使用Resources和AssetBundle).在此简单总结运行时用R ...

  5. 【转】一篇文章读懂人力资源三支柱体系(COE・BP・SSC)

    通过人力资源转型,提升效率和效能   作者:Sharon Li,翰威特大中华区咨询总监. 杰克韦尔奇曾说过“人力资源负责人在任何企业中都应该是第二号人物”,但在中国,99%的企业都做不到.原因很简单, ...

  6. drupal drush 在windows下的安装和配置

    一.windows下drupal的安装 参考官网:https://www.drupal.org/node/594744 drush下载:https://github.com/drush-ops/dru ...

  7. Hbase 学习(六) 配置文件调优

    这部分的内容,网上多了去了,都大同小异的,仅作为备忘录,省得需要的时候又要到处查. 1.zookeeper.session.timeout 默认3分钟,zookeeper和hbase通信的超时时间,设 ...

  8. js 获取页面宽度

    特例: 当$(window).width()无效时 /* 出现时机: iframe内嵌子页面在加载过程中取不到$(window).width(),非必现,机率大概1 / 20 */ 可用以下方式获取屏 ...

  9. json如何把键名作为变量?

    有时候在项目开发过程中,我们需要把json对象的键名作为一个变量.此时我们该怎么做呢? 传统的json数据格式如下: <script type="text/javascript&quo ...

  10. excel数据批量导入

    1.  html <form id="form_search" action="@Url.Action("UpLoadFile")" ...