分析:private表示派生类是更加严格的基类或者在某方面继承了基类。 它不遵守 Liskov substitution principle。
I just do not understand this error.
As I understand, and as this
tutorial confirms,
I think the private specifier is doing more than just change visibility of
|
||
feedback
|
6
|
By making the inheritance private, you're basically saying that even the fact that B inherits from A (at all) is private -- not accessible/visible to the outside world. Without getting into a long-winded discussion of what would happen if it was allowed, the simple fact is that it's not allowed. If you want to use a pointer to base to refer to an object of derived type, then you're pretty much stuck with using public inheritance. Edit: Since somebody went to the trouble of sending an email to ask for more information about what could happen if this was allowed, I guess I'll elaborate a little on it. The basic problem is that private inheritance is not necessarily intended to follow the Liskov substitution principle. Public inheritance asserts that a derived object can be substituted for an object of the base class, and proper semantics will still result. Private inheritance does not assert that though. The usual description of the relationship implied by private inheritance is "is implemented in terms of". Public inheritance means a derived class maintains all the capabilities of the base class and potentially adds more besides. Private inheritance often means more or less the opposite: that the derived class uses a general base class to implement something with a more restricted interface.
Just for example, let's assume for the moment that the containers in the C++ standard library were implemented using inheritance rather than templates. In the current system,
If we wanted to provide essentially the same with inheritance, we would probably use private inheritance, so
In this case, we definitely do not want the user to be able to manipulate our |
原址:http://stackoverflow.com/questions/9661936/inheritance-a-is-an-inaccessible-base-of-b