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++?
I want to do it because I need to overload some functions of A, without changing A code at all.
Thanks.
Quick answer: You don’t. Thats what the
protectedkey-word is for, which you want to use if you want to grant access to subclasses but no-one else.privatemeans that no-one has access to those variables, not even subclasses.If you cannot change code in
Aat all, maybe there is apublic/protectedaccess 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!).