access private members in inheritance

后端 未结 6 1355
余生分开走
余生分开走 2021-01-31 17:23

I have a class A, which have a field val declared as private. I want to declare a class B, that inherit from A and have an access to val. Is there a way to do it on C++?

<
6条回答
  •  一向
    一向 (楼主)
    2021-01-31 17:32

    Private members of a base class can only be accessed by base member functions (not derived classes). So you have no rights not even a chance to do so :)

    class Base

    • public: can be accessed by anybody
    • private: can only be accessed by only base member functions (not derived classes)
    • protected: can be accessed by both base member functions and derived classes

提交回复
热议问题