FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

怎甘沉沦 提交于 2019-11-26 22:41:19
John Dibling

Because dynamic_cast can only downcast polymorphic types, so sayeth the Standard.

You can make your class polymoprphic by adding a virtual destructor to the base class. In fact, you probably should anyway (See Footnote). Else if you try to delete a B object through an A pointer, you'll evoke Undefined Behavior.

class A
{
public:
  virtual ~A() {};
};

et voila!

Footnote

There are exceptions to the "rule" about needing a virtual destructor in polymorphic types.
One such exception is when using boost::shared_ptr as pointed out by Steve Jessop in the comments below. For more information about when you need a virtual destructor, read this Herb Sutter article.

As the other stated: The standard says so.

So why does the standard says so?

Because if the type isn't polymorphic it may (or is? Question to the standard gurus) be a plain type. And for plain types there are many assumptions coming from the C backwards compatibility. One of those is that the type only consists of it's members as the developer declared + necessary alignment bytes. So there cannot be any extra (hidden) fields. So there is no way to store in the memory space conserved by A the information that it really is a B.

This is only possible when it is polymorphic as then it is allowed to add such hidden stuff. (In most implementations this is done via the vtable).

From 5.2.7 (Dynamic cast) :

The result of the expression dynamic_cast<T>(v) is the result of converting the expression v to type T.

[ ... multiple lines which refer to other cases ... ]

Otherwise v shall be a pointer to or an lvalue of a polymorphic type (10.3).

From 10.3 (Virtual functions) :

A class that declares or inherits a virtual function is called a polymorphic class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!