a question about the precedence of C++ operators "address of" and "scope resolution"

In the first case you're taking the address of pointer-to-member B::b. Since such a pointer is NOT a member of the parent of A but a separate object, it can't access it via the protected mechanism.

In the SECOND case where it works you're asking for the address of the specific instance of b, qualifying it with its base class so that in the case of multiple inheritance the compiler would know which base class you mean. In this context the protected attribute is visible.

Note that this compiles:

class B
{
protected:
int b;
};

class A : public B
{
public:
void foo(){ &A::b; }  // Note here &A:: instead of &B::
};

As an added example it doesn't work for the same reason that the following (hopefully more familiar) code doesn't work:

class B
{
protected:
int b;
};

class A : public B
{
public:
void foo(const B* b_obj) { b_obj->b; }
};

This is just a supplementation.
§5.3.1/2 says:

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. In the first case, if the type of the expression is “T,” the type of the result is “pointer to T.” ...
For a qualified-id, ... If the member is a non-static member of class C of type T, the type of the result is “pointer to member of class C of type T.”

According to §5.1/7, B::b comes under the qualified-id case, but (B::b) doesn't. So, compiler interprets it as an lvalue.


The differece between the two statements becomes more obvious when you try and return the value:

int*     foo()    { return &(B::b);}  // This is a pointer to an int


int A::* foo()    { return &B::b; }   // This is a pointer to a member of type int

What you want to do is access it via the A object:

int A::* foo()    { return &A::b; }   // This is a pointer to a member of type int

As from the A you are allowed to access it.
Accessing it via B like that is accessing it from outside and thus triggers the access specifiers.