Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What are the access rules with “private” and “protected” inheritance?

0
Posted

What are the access rules with “private” and “protected” inheritance?

0

Take these classes as examples: class B { /*…*/ }; class D_priv : private B { /*…*/ }; class D_prot : protected B { /*…*/ }; class D_publ : public B { /*…*/ }; class Client { B b; /*…*/ }; Public and protected parts of B are “private” in D_priv, and are “protected” in D_prot. In D_publ, public parts of B are public (D_prot is-a-kind-of-a B), and protected parts of B remain protected in D_publ. Naturally *none* of the subclasses can access anything that is private in B. Class “Client” can’t even access the protected parts of B (ie: it’s “sealed off”). It is often the case that you want to make some but not all inherited member functions public in privately/protectedly derived classes. Ex: to make member fn B::f(int,char,float) public in D_prot, you would say: class D_prot : protected B { //… public: B::f; //note: not B::f(int,char,float) }; There are limitations to this technique (can’t distinguish overloaded names, and you can’t make a feature that was “protected” in the ba

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123