As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Because you can't see it:
So you can not use a PrivateDerived object where a Base object could be used.
So it will never act like a Base class object.
It can certainly be considered an object of the super-class. However, such consideration is restricted (by the public/protected/private inhertiance modifier) to but only by itself (private inheritance) or it's sub-classes (protected inheritance).
All external objects are not allowed to considered the class as such, similar to how they not allowed to access protected or private methods or variables. The analogy is rather fitting, if expressed properly.
So, the class itself, its subclasses (and friends) can see this as an is-a relationship, but the outside world is not permitted to do so.
The following code shows this in action:
Note that it doesn't matter how the xxxSubClass-classes derive from their super-classes. It's all about how the super-classes derive from Base, which is as it should be.
The compiler complains appropriately:
You can think of public / protected / private inheritance like accessibility for any class member : it a matter of 'how much you want to show'.
A private (or protected, in a slightly different way) inheritance is a relationship which is not shown the outside world. As such, you can't treat an object of a derived type as its private base, because you don't get to "see" that this relationship even exists.
In general you will find in literature (and in other answers here) that
protected
/private
inheritance imply that the class cannot be used as abase
. The fact (some other answer hints into this) is that only the visibility of the inheritance is affected by the operation. Thederived
class is abase
class, even if external code cannot see it.Any
friend
or the class will be able to make use of that relationship:Now, while technically this is the case, semantically when you use
private
inheritance you are trying to model not anis-a
but rather aimplemented-in-terms-of
relationship. This is the important part: while reading code, if you seeprivate
inheritance you should not think on is-a but implemented-in-terms-of.The "why" is simple when considering how the mechanism works: because protected and private inheritance are meant to work that way.
This is probably not enough to answer the question's intent though. You might ask "and why have private and protected inheritance if you can't use the resulting objects as instances of the base class?"
Well, non-public inheritance is meant to facilitate the "is implemented in terms of" relationship between two classes (whereas public inheritance facilitates the "is-a" relationship). In other words, you intend to reuse part or all of the base class functionality to provide services to your own consumers.
This scenario is almost always better implemented by aggregation instead of inheritance (i.e., having a member object of the "base" class), and I would go so far as to say that non-public inheritance is something better left alone.
Take a look at this for a longer write-up that expands on the above.
Update: as the commenters below state, there are some (admittedly rare) cases where non-public inheritance provides the mechanism for architectural functionality that would not otherwise be possible. Do read them, as exploring the edges of a language can be quite enlightening. But try to do it as little as you can anyway.
In brief, because private inheritance is inheritance of implementation, not that of interface. A private subclass
Derived
object is not aBase
, but is implemented in terms ofBase
. The public and protected members ofBase
are visible forDerived
, but they become private, thus inaccessible for the outside world. Thus private inheritance can be thought of as a special form of composition, which is in fact rarely needed in practice. (And protected inheritance is practically never - in fact probably even Bjarne Stroustrup doesn't know what protected inheritance means.)