Victor Andreikin
Victor Andreikin

Reputation: 13

Is possible to "unprivate" an element in C++ inheritance? What to do if not?

If i want to inherit from "BaseClass" but i have to manage one of the private members, can i "unprivate" it with something like this?:

using BaseClass::private_member;

If not, what to do when some of the members are NOT marked as protected as it should be? If is not possible, that means we are not supposed to inherit from classes that we did not develop?

Upvotes: 1

Views: 59

Answers (1)

JaMiT
JaMiT

Reputation: 16997

If i want to inherit from "BaseClass" but i have to manage one of the private members, can i "unprivate" it with something like this?

No. Private members cannot be managed by derived classes. If you think you have to manage a private member of a base class, then someone's design or implementation is wrong. It might be the base class that is flawed, but keep an open mind – be sure to consider the possibility that the flaw is in your design. (Better yet, assume the flaw is probably in your design until proven otherwise.)

If not, what to do when some of the members are NOT marked as protected as it should be?

First, verify this assumption. Should the member be marked protected, or are you trying to misuse the base class? If the member truly should be marked as protected, then the thing to do is fix the base class. File a bug report if the base class is not under your control.

If is not possible, that means we are not supposed to inherit from classes that we did not develop?

No, that means you should not build upon a flawed foundation. (Or possibly it means that you should work with the base class design instead of against it.) There are plenty of well-implemented base classes out there. Use the right tool for the job at hand.

Upvotes: 1

Related Questions