May pointer to members circumvent the access level of a member?

后端 未结 2 352
盖世英雄少女心
盖世英雄少女心 2020-12-28 08:51

Our infamous litb has an interesting article on how to circumvent the access check.

It is fully demonstrated by this simple code:

#include 

        
2条回答
  •  感动是毒
    2020-12-28 09:16

    The code is clearly illegal (and requires a compile time diagnostic). In the line:

    template struct Rob;
    

    the expression A::a accesses a private member of A.

    The standard is very clear about this: “Access control is applied uniformly to all names, whether the names are referred to from declarations or expressions.“ (§11/4, emphasis added). Since a is a private name in A, any reference to it outside of A is illegal.

提交回复
热议问题