access private members in inheritance

后端 未结 6 1365
余生分开走
余生分开走 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:39

    Quick answer: You don't. Thats what the protected key-word is for, which you want to use if you want to grant access to subclasses but no-one else.

    private means that no-one has access to those variables, not even subclasses.

    If you cannot change code in A at all, maybe there is a public/protected access method for that variable. Otherwise these variables are not meant to be accessed from subclasses and only hacks can help (which I don't encourage!).

提交回复
热议问题