无法从派生类型的范围访问另一个实例的受保护成员 [英] Cannot access protected member of another instance from derived type's scope

查看:97
本文介绍了无法从派生类型的范围访问另一个实例的受保护成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的答案中,"为什么我的对象无法访问定义的另一个对象的受保护成员"在普通的基类中?",可以读到:

In this answer to the question "Why can't my object access protected members of another object defined in common base class?", one can read:

您只能从自己的基类实例访问受保护的成员.

You can only access protected members from your own base class instance.

我无法正确获取它,或者跟随MCVE(在coliru上运行)证明是错误的:

Either I don't get it correctly or the following MCVE (live on coliru) proves it wrong:

struct Base           { void f(); protected: int prot; };
struct Derived : Base { void g(); private:   int priv; };

void Base::f()
{
    Base b;
    b.prot = prot;
    (void) b;
}

void Derived::g()
{
    {
        Derived d;
        (void) d.priv;
    }

    {
        Derived& d = *this;
        (void) d.priv;
    }

    {
        Derived d;
        (void) d.prot; // <-- access to other instance's protected member
    }

    {
        Derived& d = *this;
        (void) d.prot;
    }

    // ---

    {
        Base b;
        (void) b.prot; // error: 'int Base::prot' is protected within this context
    }

    {
        Base& b = *this;
        (void) b.prot; // error: 'int Base::prot' is protected within this context
    }
}

鉴于这两个错误,我想知道:为什么我可以从Derived的范围访问另一个Derived实例的受保护成员,但是无论如何都不能从相同的范围访问另一个Base实例的受保护成员. Derived源自Base的事实? Tl; dr:在这种情况下,是什么使protectedprivate更私有"?

In the light of the two errors I get to wonder: why can I access to another Derived instance's protected member from the scope of Derived but cannot access to another Base instance's protected member from the same scope regardless of the fact that Derived devires from Base? Tl; dr: what makes protected more "private" than private in this case?

注释:

  • 请不要关闭该问题,不要重复链接该问题;
  • 欢迎使用更好的标题建议.

推荐答案

[class.access.base] 是:

如果在类N中命名为[...]

A member m is accessible at the point R when named in class N if [...]

    作为N成员的
  • m受保护,并且 R 出现在类N的成员或朋友中,或者出现在从<派生的类P的成员中c8>,其中m作为P的成员是publicprivateprotected
  • m as a member of N is protected, and R occurs in a member or friend of class N, or in a member of a class P derived from N, where m as a member of P is public, private, or protected

里面有很多字母.但是基本上有两个条件:

There's a lot of letters in there. But there are basically two conditions:

  1. R是该课程的成员或朋友.这处理了d.prot的示例-访问Derived的受保护成员时,我们位于Derived的成员中.
  2. R在派生类的成员中,被访问的成员是派生类实例的的成员.这处理了b.prot示例-我们在派生类的成员中,但是prot不是派生类的成员.
  1. R is in a member or friend of the class. This handles the d.prot example - we are in a member of Derived while accessing a protected member of Derived.
  2. R is in a member of a derived class and the member being accessed is a member of the derived class instance. This handles the b.prot example - we are in a member of a derived class, but prot is not a member of the derived class.

换句话说,Derived可以访问Base的受保护成员-但仅在它正在访问其自己的子对象的受保护成员的情况下.它无法访问其他Base对象的受保护成员.当您认为另一个Base很容易成为SomeOtherDerived时,这很有意义,在这种情况下,这对我们来说是另一个无关的对象,我们没有特殊的访问权限.

In other words, Derived can access Base's protected members - but only in the case that it is accessing its own subobject's protected members. It cannot access other Base object's protected members. This makes sense when you consider that this other Base could easily be SomeOtherDerived, in which case that's just another unrelated object to us that we have no special access privileges to.

这篇关于无法从派生类型的范围访问另一个实例的受保护成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆