Can a friend class object access base class private members on a derived class object?

后端 未结 5 1845
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 21:39

I\'m surprised that the code below compiles.

It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an in

5条回答
  •  一生所求
    2021-02-19 22:24

    While there already are good answers I think some images would help here a bit too.

    This is an abstraction of your B class. F has access to all its members.

    When you now instantiate a D object it looks like this

    It still is a B object but also a D object. It extends B so to speak. F can still access the part from B because it's still there but not from D.

    Please note that these abstractions do not really display the layout in memory and explain overriding etc. But they are just for the sake of this question.

提交回复
热议问题