一个很简单的问题:

//为什么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. 4. EM算法-高斯混合模型GMM详细代码实现

    1. EM算法-数学基础 2. EM算法-原理详解 3. EM算法-高斯混合模型GMM 4. EM算法-高斯混合模型GMM详细代码实现 5. EM算法-高斯混合模型GMM+Lasso 1. 前言 EM ...

  2. python学习笔记十四:wxPython Demo

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...

  3. python 全局变量的使用

    我尝试使用 类似 C 语言的方式去调用 python 的全局变量,发现不行,后经过 尝试,要使用 global 进行调用 test_num = 0; # 首先声明一个全局变量 def test_fun ...

  4. 如何使用sendEmail发送邮件

    sendEmail是一个轻量级,命令行的SMTP邮件客户端.如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php.bashperl和web站 ...

  5. Oracel扩展表空间

    --表空间查看 SELECT tbs, sum(totalM) as total, sum(usedM) as UserdM, sum(remainedM) as remainedM, as User ...

  6. 【linux】——linux僵死进程的产生与避免

    一个进程在调用exit命令结束自己的生命的时候,其实它并没有真正的被销毁, 而是留下一个称为僵死进程(Zombie)的数据结构(系统调用exit,它的作用是使进程退出,但也仅仅限于将一个正常的进程变成 ...

  7. 【C】——dup/dup2用法

    相信大部分在Unix/Linux下编程的程序员手头上都有<Unix环境高级编程>(APUE)这本超级经典巨著.作者在该书中讲解dup/dup2之前曾经讲过“文件共享”,这对理解dup/du ...

  8. js学习(一)-对象和函数概念

    //-----------------------js代码-------------------- function class1(){       //类成员的定义及构造函数 this.name = ...

  9. 关于Unity中的光照(五)

    Mobile Diffuse Unity自带的一种shader,用的比较多,性能还可以.我们默认创建的unit shader基本和它一致,但是没有参与光照计算,看起来和Mobile Diffuse有区 ...

  10. SpagoBI 教程 Lesson 5: Creating a dashboard with BIRT and SpagoBI

    SpagoBI Lesson 5: Creating a dashboard with BIRT and SpagoBI Creating a dashboard with BIRT and Spag ...