dynamic_cast的用途是什么? [英] what is the use of dynamic_cast?

查看:100
本文介绍了dynamic_cast的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



什么是dynamic_cast?我在哪里可以使用dynamic_cast?



请描述一下?



问候,

Ranjith

Hi All,

what is dynamic_cast? and where can i use dynamic_cast?

Please describe?

Regards,
Ranjith

推荐答案

首先让我们定义一些概念:类层次结构,向上转换,向下转换。

UML中的类层次结构通常是可视化的作为一个类树(有时是图形),这张图片说明的方式: http:// www.ccs.neu.edu/research/demeter/personalities/luis/example/Image1.gif [ ^ ]

正如您所见,树的根是基类,它位于最重要的是。如果你有一个指向派生类实例的指针,并将指针转换为其某个基类的指针,那么你正在进行向上转换,因为你从层次结构树的一个较低节点转换到另一个节点。在树上更高。这种类型的强制转换是安全和直接的,因此您甚至不必为编译器指示此强制转换,它将自动执行强制转换:

First lets define a few concepts: class hierarchy, upcast, downcast.
A class hierarchy in UML is often visualized as a class tree (sometimes graph) the way this picture illustrates: http://www.ccs.neu.edu/research/demeter/personalities/luis/example/Image1.gif[^]
As you see the root of the tree is the base class and it is at the top of the picture. If you have a pointer to a derived class instance and you cast the pointer into a pointer of one of its base classes then you are doing an "upcast" because you cast from one of the lower nodes of the hierarchy tree to another node that is higher in the tree. This type of cast is safe and straightforward so you don''t even have to indicate this cast for the compiler, it will do the cast automatically:
class Base {};
class Derived : public Base {};
Derived* d = blahblah;
Base* b = d;    // automatic upcast



但是当你从基类转换为派生类,它被称为被视为不安全的向下转换。编译器不知道Animal类型的基类指针是指向Tiger实例还是鼠标实例......但如果基类有vtable(因为它至少有1个虚方法),那么你可以使用dynamic_cast要求编译器生成一些代码,以确定您的基类指针是否指向特定的子类实例:


However when you cast from a base class to a derived class it is called a downcast that is considered unsafe. The compiler doesn''t know whether a base class pointer of type "Animal" points to a Tiger instance or a Mouse instance... But if the base class has a vtable (because it has at least 1 virtual method) then you can use dynamic_cast to ask the compiler to generate some code that finds out whether your base class pointer points to a particular subclass instance or not:

class Base
{
    // making sure that this class hierarchy has a vtable
    // because the vtable contains the typeinfo needed by dynamic_cast
    virtual ~Base() {}
};
class Derived1 : public Base {};
class Derived2 : public Base {};
Base* b = new Derived1;
Derived1* d1 = dynamic_cast<derived1*>(b);  // d1 will be non-NULL
Derived2* d2 = dynamic_cast<derived2*>(b);  // d2 will be NULL



dynamic_cast使用在vtable中找到的typeinfo(通常在方法指针下方的负偏移处),这就是为什么你的基类必须至少有一个虚函数。



最后的笔记:使用dynamic_cast(以及在任何面向对象语言的编程中一般的向下转换)都会破坏polimorphism的目的。它就像使用if-then-else而不是真正的面向对象设计。在面向对象程序中使用向下转换(C ++中的dynamic_cast)是错误代码/ OO设计的强有力指标。另一个缺点是,与其他类型的安全演员相比,dynamic_cast可能相对较慢。

如果您的代码中包含dynamic_casts,那么您的OO设计很糟糕!我允许每年最多使用一次dynamic_cast,只有在修补程序的情况下,当一些脏的黑客被排除而不是重新设计并且代码不是性能关键时。


dynamic_cast uses the typeinfo found in the vtable (usually at a negative offset below the method pointers) thats why your base class has to have at least one virtual function.

Final notes: using dynamic_cast (and downcast in general in programming in any object oriented language) defeats the purpose of polimorphism. Its like using if-then-else instead of true object oriented design. Using downcasts in an object oriented program (dynamic_cast in C++) is a strong indicator of bad code/OO design. Another drawback is that dynamic_cast can be relatively slow compared to other kind of safe casts.
If your code has dynamic_casts in it then your OO design sucks! I allow using dynamic_cast for myself at most once a year only in case of hotfixes when a little dirty hack is excepted instead of a redesign and the code is not performance critical.


dynamic_cast几乎是唯一的用于处理多态性。您可以将指向任何多态类型的指针或引用转换为任何其他类类型(多态类型至少具有一个虚函数,声明或继承)。你不必用它来向下投掷,你可以侧身或甚至向上投掷另一条链。 dynamic_cast将寻找所需的对象并在可能的情况下返回它。如果它不能,它将在指针的情况下返回NULL,或者在引用的情况下抛出std :: bad_cast。



dynamic_cast有一些然而,局限性。如果在继承层次结构中存在多个相同类型的对象(所谓的可怕的钻石)并且您没有使用虚拟继承,则它不起作用。它也只能通过公共继承 - 它总是无法通过受保护或私有继承。然而,这很少是一个问题,因为这种继承形式很少见。



参考: stackoverflow.com [ ^ ]
dynamic_cast is almost exclusively used for handling polymorphism. You can cast a pointer or reference to any polymorphic type to any other class type (a polymorphic type has at least one virtual function, declared or inherited). You don''t have to use it to cast downwards, you can cast sideways or even up another chain. The dynamic_cast will seek out the desired object and return it if possible. If it can''t, it will return NULL in the case of a pointer, or throw std::bad_cast in the case of a reference.

dynamic_cast has some limitations, though. It doesn''t work if there are multiple objects of the same type in the inheritance hierarchy (the so-called ''dreaded diamond'') and you aren''t using virtual inheritance. It also can only go through public inheritance - it will always fail to travel through protected or private inheritance. This is rarely an issue, however, as such forms of inheritance are rare.

Reference : stackoverflow.com[^]


这篇关于dynamic_cast的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆