且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

dynamic_cast如何工作?

更新时间:2022-06-23 23:11:25

关于动态类型转换的最重要的一点是,它应该应用于多态类型.否则,动态转换的工作方式就类似于静态转换.

The most important thing about the dynamic cast is that it should be applied to a polymorphic type. Without that, dynamic cast works like a static cast.

什么是多态类型?具有至少一个虚拟方法或虚拟析构函数或虚拟基类的任何类都是多态的.仅这些类型的数据布局中具有虚拟方法表(VMT).没有虚拟类的类没有VMT.该标准没有说明应如何实现多态和虚拟方法,但据我所知,所有编译器都可以做到这一点.

What is a polymorphic type? Any class that has at least one virtual method or virtual destructor or virtual base class is polymorphic. Only those types have a virtual method table (VMT) in their data layout. Classes that do not have anything virtual do not have VMT's. The standard does not say how polymorphism and virtual methods should be implemented, yet all compilers, as far as I know, do this.

在您的示例中,类不是多态的.我认为,如果将动态类型转换应用于非多态类型,则编译器发出错误会更好.但是,他们不这样做.这加剧了混乱.

In your examples classes are not polymorphic. In my opinion, it would be better if compilers would issue an error when the dynamic cast is applied to a non-polymorphic type. Nevertheless, they do not do this. This adds to the confusion.

VMT指针是不同的.这意味着在运行时查看:

VMT pointers for all classes are different. This means that on the runtime looking at:

Animal* animal;

有可能知道对象的真实类是什么.是 Bird 还是 Dog 还是其他东西.从VMT的值知道真实类型,如果需要,生成的代码可以进行调整.

it is possible to know what the real class of the object is. Is it a Bird or a Dog or something else. Knowing the real type from the value of VMT, generated code can make an adjustment if this is needed.

这里是一个例子:

class Animal   { virtual ~Animal();   int m1; };
class Creature { virtual ~Creature(); int m2; };

class Bird : public Animal, Creature { };

Bird *bird = new Bird();
Creature *creature = dynamic_cast<Creature*>(bird);

请注意,生物不是第一基础类别.这意味着指针将移动以指向对象的右侧.尽管如此,以下内容仍将起作用:

Note that creature is not the first base class. This means that the pointer will be shifted to point to the right part of the object. Nevertheless, the following will still be working:

Animal *animal = dynamic_cast<Animal*>(creature);   // Case2.

因为Creature属于其他类的VMT与单独使用该对象时的VMT并不相同:

because VMT of Creature when it is part of other class will not be the same to VMT of the object when it is used stand-alone:

Creature *creature1 = new Creature();

这种区别允许动态转换的正确实现.在示例 Case2 中,指针将向后移.我测试了这个.这行得通.

This distinction allows a proper implementation of a dynamic cast. In the example Case2 the pointer will be shifted back. I tested this. This works.