14

我正在阅读这篇文章,不幸的是无法深入理解为什么编译器不允许从 Derived** 转换为 Base**。我也看到了这个,它提供的信息不比 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()!
4

3 回答 3

22

这与一碗香蕉不是一碗水果的原因基本相同。如果一碗香蕉一碗水果,你可以把一个苹果放进碗里,它就不再是一碗香蕉了。

只要您只检查碗,转换是无害的。但是一旦你开始修改它,转换就变得不安全了。这是要牢记的关键点。(这就是为什么不可变 Scala 集合实际上允许转换,但可变集合禁止它的确切原因。)

和你的例子一样。如果有从Derived**to的转换Base**,您可以放置​​一个指向苹果的指针,因为类型系统承诺只存在指向香蕉的指针。繁荣!

于 2011-11-06T09:10:53.360 回答
11

这将允许出现无意义的错误:

class Flutist : public Musician
...

class Pianist : public Musician
...

void VeryBad(Flutist **f, Pianist **p)
{
 Musician **m1=f;
 Musician **m2=p;
 *m1=*m2; // Oh no! **f is supposed to be a Flutist and it's a Pianist!
}

这是一个完整的工作示例:

#include <stdio.h>

class Musician
{
 public:
 Musician(void) { ; }
 virtual void Play(void)=0;
};

class Pianist : public Musician
{
 public:
 Pianist(void) { ; }
 virtual void Play(void) { printf("The piano blares\n"); }
};

class Flutist : public Musician
{
 public:
 Flutist(void) { ; }
 virtual void Play(void) { printf("The flute sounds.\n"); }
};

void VeryBad(Flutist **f, Pianist **p)
{
 Musician **m1=f;
 Musician **m2=p;
 *m1=*m2; // Oh no! **f is supposed to be a Flutist and it's a Pianist!
}

int main(void)
{
 Flutist *f=new Flutist();
 Pianist *p=new Pianist();
 VeryBad(&f, &p);
 printf("Mom is asleep, but flute playing wont bother her.\n");
 f->Play(); // Since f is a Flutist* this can't possibly play piano, can it?
}

它在行动中:

$ g++ -fpermissive verybad.cpp -o verybad
verybad.cpp: In function void VeryBad(Flutist**, Pianist**):
verybad.cpp:26:20: warning: invalid conversion from Flutist** to Musician** [-fpermissive]
verybad.cpp:27:20: warning: invalid conversion from Pianist** to Musician** [-fpermissive]
$ ./verybad 
Mom is asleep, but flute playing wont bother her.
The piano blares
于 2011-11-06T09:09:59.507 回答
0

Vehicle** vehiclePtrPtr = carPtrPtr;是不允许的,因为它是一个Derived**toBase**转换,这是不允许的。

您的示例说明了不允许这样做的原因。

   Car   car;
   Car*  carPtr = &car;
   Car** carPtrPtr = &carPtr; 

所以它carPtrPtr指向一个指向Car.

核潜艇潜艇;核潜艇* subPtr = ⊂

这也是合法的,但如果你能做到

Vehicle** vehiclePtrPtr = carPtrPtr;

你可能会不小心做

*vehiclePtrPtr = subPtr;

那是*vehiclePtrPtr一个指向 a 的指针Car。在最后一行中,您为其分配了一个指向 a 的指针SubSub因此,您现在可以在类型为的对象上调用派生类中定义的方法Car,但行为未定义。

于 2013-05-04T22:01:16.217 回答