开发者

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

开发者 https://www.devze.com 2023-02-11 08:30 出处:网络
Hello I have this code with a compiler error (error is from Microsoft Visual Studio 2008): class B { protected:

Hello I have this code with a compiler error (error is from Microsoft Visual Studio 2008):

class B {
protected:
    int b;
};

class A : public B {
public:
    void foo() { &B::b; } 
// error C2248: 'B::b' : cannot access protected member declared in class 'B'
};

while this code is error free:

class B {
protected:
    int b;
};

class A : public B {
public:
    void foo() { &(B::b); }
}开发者_运维问答;

The two snippets seem to me equivalent based on my knowledge of the precedence of operators, because :: has an higher precedence than & (see for example table 2 at page 137 of "JOINT STRIKE FIGHTER AIR VEHICLE C++ CODING STANDARDS FOR THE SYSTEM DEVELOPMENT AND DEMONSTRATION PROGRAM" )

But they are different... I think it is something related to "pointer-to-data-member" but I do not know how does it fit with the operators precedence.

Any explanation?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消