派生类访问类型疑问:public与protected继承的差异是什么
Great question! Let’s cut straight to the chase—you’ve already nailed that both inheritance modes let the derived class access the base class’s public and protected members internally. The core difference lies entirely in how those inherited members are exposed to code outside the derived class (and to future subclasses of the derived class).
Here’s the breakdown:
Public inheritance (
class derived: public base):
This is the classic "is-a" relationship inheritance. The base class’spublicmembers staypublicin the derived class, andprotectedmembers stayprotected. This means:- External code (like
main()or unrelated functions) can directly call the base class’s originalpublicmembers on a derived class object. - If you create a subclass of this derived class later, it will inherit the base class’s members with their original access levels (assuming the subclass uses public inheritance too).
- External code (like
Protected inheritance (
class derived: protected base):
This is a more encapsulated inheritance mode. The base class’spublicandprotectedmembers both becomeprotectedin the derived class. This changes things for external code:- External code cannot directly access any of the inherited base class members (even the original
publicones) on a derived class object. Only the derived class’s own member functions, its friends, and its future subclasses can access these members. - Any subclass of this derived class will see the original base class members as
protected(regardless of the subclass’s inheritance mode), since they’re inherited through the protected layer.
- External code cannot directly access any of the inherited base class members (even the original
Example to Drive It Home
Let’s look at a quick code snippet that shows the practical difference:
#include <iostream> class Base { public: void publicBaseFunc() { std::cout << "Base public method\n"; } protected: void protectedBaseFunc() { std::cout << "Base protected method\n"; } }; // Public inheritance class DerivedPublic : public Base { public: void useBaseMethods() { publicBaseFunc(); // Accessible (still public) protectedBaseFunc(); // Accessible (still protected) } }; // Protected inheritance class DerivedProtected : protected Base { public: void useBaseMethods() { publicBaseFunc(); // Accessible (now protected) protectedBaseFunc(); // Accessible (still protected) } }; int main() { DerivedPublic dp; dp.publicBaseFunc(); // ✅ Works! Public member stays public // dp.protectedBaseFunc(); // ❌ Error: Protected, can't access externally DerivedProtected dpt; // dpt.publicBaseFunc(); // ❌ Error! Now protected, external code can't access dpt.useBaseMethods(); // ✅ Works: Derived class's own method can access internal members return 0; }
To Sum Up
Public inheritance preserves the base class’s interface for external use, making the derived class a true subtype of the base. Protected inheritance hides the base class’s entire interface from the outside world, only keeping it accessible for the derived class and its own descendants.
内容的提问来源于stack exchange,提问作者Saif Faidi

