从派生**到基础**的转换
我正在阅读此,不幸的是无法理解深入了解为什么编译器不允许从 Derived** 到 Base** 的转换。我还看到 this 没有给出比 parashift.com 的链接有更多信息。
编辑:
让我们逐行分析这段代码:
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
//MyComment: Until now there is no problem!
Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++
//MyComment: Here compiler gives me an error! And I try to understand why.
//MyComment: Let us consider that it was allowed. So what?? Let's go ahead!
NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
//MyComment: this two line are OK too!
*vehiclePtrPtr = subPtr;
//MyComment: the important part comes here... *vehiclePtrPtr is a pointer to
//MyComment: a vehicle, particularly in our case it points to a Car object.
//MyComment: Now when I assign to the pointer to the Car object *vehiclePtrPtr,
//MyComment: a pointer to NuclearSubmarine, then it should just point to the
//MyComment: NuclearSubmarine object as it is indeed a pointer to a Vehicle,
//MyComment: isn't it? Where is my fault? Where I am wrong?
// This last line would have caused carPtr to point to sub!
carPtr->openGasCap(); // This might call fireNuclearMissle()!
I was reading this and unfortunately could not understand in depth why the compiler does not allow conversion from Derived** to Base**. Also I have seen this which gives no more info than the parashift.com's link.
EDIT:
Let us analyze this code line by line:
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
//MyComment: Until now there is no problem!
Vehicle** vehiclePtrPtr = carPtrPtr; // This is an error in C++
//MyComment: Here compiler gives me an error! And I try to understand why.
//MyComment: Let us consider that it was allowed. So what?? Let's go ahead!
NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
//MyComment: this two line are OK too!
*vehiclePtrPtr = subPtr;
//MyComment: the important part comes here... *vehiclePtrPtr is a pointer to
//MyComment: a vehicle, particularly in our case it points to a Car object.
//MyComment: Now when I assign to the pointer to the Car object *vehiclePtrPtr,
//MyComment: a pointer to NuclearSubmarine, then it should just point to the
//MyComment: NuclearSubmarine object as it is indeed a pointer to a Vehicle,
//MyComment: isn't it? Where is my fault? Where I am wrong?
// This last line would have caused carPtr to point to sub!
carPtr->openGasCap(); // This might call fireNuclearMissle()!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这基本上和一碗香蕉不是一碗水果的原因是一样的。如果一碗香蕉是一碗水果,你可以把一个苹果放进碗里,它就不再是一碗香蕉了。
只要您只检查碗,这种转换是无害的。但一旦您开始修改它,转换就会变得不安全。这是要牢记的关键点。 (这就是为什么不可变的 Scala 集合实际上允许转换,但可变集合禁止转换的确切原因。)
与您的示例相同。如果存在从
Derived**
到Base**
的转换,则您可以放置一个指向苹果的指针,而类型系统承诺只能存在指向香蕉的指针。繁荣!It's basically the same reason why a bowl of bananas is not a bowl of fruits. If a bowl of bananas were a bowl of fruits, you could put an apple into the bowl, and it would no longer be a bowl of bananas.
As long as you only inspect the bowl, the conversion is harmless. But as soon as you start modifying it, the conversion becomes unsafe. This is the key point to bear in mind. (This is the precise reason why the immutable Scala collections actually allow the conversion, but the mutable collections prohibit it.)
Same with your example. If there was a conversion from
Derived**
toBase**
, you could put a pointer to an apple were the type system promised only a pointer to a banana could exist. Boom!这将允许不乏无意义的错误:
这是一个完整的工作示例:
这里是它的实际应用:
There are no shortage of senseless errors this would permit:
Here is a full working example:
And here it is in action:
不允许
Vehicle**vehiclePtrPtr = carPtrPtr;
,因为它是Derived**
到Base**
的转换,这是不允许的。您的示例中说明了不允许的原因。
这样
carPtrPtr
就指向Car
的指针。核潜艇;
NuclearSubmarine* subPtr = ⊂
这也是合法的,但如果您可以这样做,
您可能会意外地
这样做,因为
*vehiclePtrPtr
是指向Car
的指针。在最后一行中,您为其分配一个指向Sub
的指针。因此,您现在可以在Car
类型的对象上调用派生类Sub
中定义的方法,但具有未定义的行为。Vehicle** vehiclePtrPtr = carPtrPtr;
is not allowed because it is aDerived**
toBase**
conversion, which is not allowed.Reason why it is not allowed is illustrated in your example.
so that
carPtrPtr
points to a pointer toCar
.NuclearSubmarine sub;
NuclearSubmarine* subPtr = ⊂
this is legal also, but if you could do
you could accidentaly do
that is
*vehiclePtrPtr
is a pointer to aCar
. with this last line, you assign to it a pointer to aSub
. Thus, you could now call a method defined in derived classSub
on a object of typeCar
, with undefined behavior.