subtle C++ inheritance error with protected fields

后端 未结 6 921
無奈伤痛
無奈伤痛 2020-12-03 02:38

Below is a subtle example of accessing an instance\'s protected field x. B is a subclass of A so any variable of type B is also of type A. Why can B::foo() access b\'s x fie

6条回答
  •  独厮守ぢ
    2020-12-03 02:52

    Class B is not identical to class A. That's why members of class B cannot access non-public members of class A.

    On the other hand, class B derives publicly from class A, so class B now has a (protected) member x which any member of class B can access.

提交回复
热议问题