[英]Conversion from Derived** to Base**
我正在阅读此书 ,不幸的是无法深入了解为什么编译器不允许从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()!
一碗香蕉不是一碗水果,这基本上是相同的原因。 如果一碗香蕉是一碗水果,则可以在碗中放一个苹果,而不再是一碗香蕉。
只要您只检查碗,转换是无害的。 但是,一旦您开始对其进行修改 ,转换将变得不安全。 这是要牢记的关键点。 (这就是不可变的Scala集合实际上允许转换但可变的集合禁止转换的确切原因。)
与您的示例相同。 如果从Derived**
到Base**
进行了转换,则可以在类型系统承诺仅存在指向香蕉的指针的情况下放置一个指向Apple的指针。 繁荣!
这将不乏无意义的错误:
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
Vehicle** vehiclePtrPtr = carPtrPtr;
不允许,因为它是Derived**
到Base**
转换,这是不允许的。
您的示例中说明了不允许这样做的原因。
Car car;
Car* carPtr = &car;
Car** carPtrPtr = &carPtr;
这样carPtrPtr
指向Car
的指针。
核潜艇潜艇; 核潜艇* subPtr =⊂
这也是合法的,但如果可以的话
Vehicle** vehiclePtrPtr = carPtrPtr;
你可能会意外地做
*vehiclePtrPtr = subPtr;
*vehiclePtrPtr
是指向Car
的指针。 在最后一行中,您为它分配了一个Sub
的指针。 因此,您现在可以在Car
类型的对象上以未定义的行为调用在派生类Sub
定义的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.