Unable to access protected member of another instance from scope type

In this answer to the question “Why can't my object protect the protected members of another object defined in a common base class?”, You can read:

Access to protected members is available only from their own instance of the base class.

Either I don't get it right, or after 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 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: protected "private" private ?

:

  • , ;
  • .
+3
1

[class.access.base]:

m R, N, [...]

  • m N , R N P, N, m P public, private protected

. :

  • R . d.prot - Derived, Derived.
  • R , . b.prot - , prot .

, Derived Base , , . , Base. , , Base SomeOtherDerived, , .

+3

Source: https://habr.com/ru/post/1695488/


All Articles