protected: C++ access control works on per-class basis, not on per-object basis

本文详细解析了C++中基类受保护成员如何在派生类中被访问,解释了访问控制的工作原理及实例演示。

一个很简单的问题:

//为什么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. 
      }
}; 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值