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

后端 未结 5 1859
佛祖请我去吃肉
佛祖请我去吃肉 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

    D is a B when public inheritance is used. So accessing b_var is still perfectly legal.
    You would get an error, however, if you attempt to access d_var, since the friendship itself is not inherited, as you seem to be aware.

    Inheritance always makes all members of the base be members of the derived. Access specifiers only affect where an identifier is visible. That's why accessing a private member illegally produces a different error to accessing an identifier that doesn't exist.

提交回复
热议问题