Why does C++ not allow inherited friendship?

前端 未结 10 1573
终归单人心
终归单人心 2020-11-28 04:11

Why is friendship not at least optionally inheritable in C++? I understand transitivity and reflexivity being forbidden for obvious reasons (I say this only to head off sim

10条回答
  •  孤城傲影
    2020-11-28 04:38

    C++ Standard, section 11.4/8

    Friendship is neither inherited nor transitive.

    If friendship would be inherited, then a class that wasn't meant to be a friend would suddenly have access to your class internals and that violates encapsulation.

提交回复
热议问题