Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • @user463035818 g = &x;? No. &x is with type int*, which can't be assigned to pointer to member. LIVE Commented Aug 13, 2018 at 14:55
  • 1
    The reason for this is that a derived class is only allowed to access the base protected members for its own type. Imagine a Shape base class with protected members. If Square inherits from it, we would not want Square to access the protected members of a Circle. (Point being, just because X inherits from Base doesn't mean X should have access to all Base protected members, only from those that are actually in use by objects of type X.) The base::qualification no longer is in the context of a derived class, and so the protected access prevents it. Commented Aug 13, 2018 at 14:57
  • there seems to be a difference between &(foo::x) and &foo::x i wasnt aware of (first is a int*, second is a pointer to int member) Commented Aug 13, 2018 at 15:03
  • @user463035818 Yes. For this case &(derived::x) is same as &(x) and &x. Commented Aug 13, 2018 at 15:09
  • 1
    @Rick Protected allows access to members (and friends) of the class that declares it, and also to its derived classes. What is NOT allowed is if X is a base with a protected member, and two classes A and B both derive from X. A can see protected member of X only when looking at classes of type A (since it is its own data), but not X's protected members in class B, and vice versa. Given a pointer to base, you don't know if it actually points to A or B, and so to disallow A from seeing B's protected data (and vice versa) it's just not allowed. Commented Apr 23, 2020 at 15:32