Can't downcast because class is not polymorphic?

后端 未结 5 1815
时光取名叫无心
时光取名叫无心 2020-12-04 23:32

Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic.

Example:

Class A(){
           


        
5条回答
  •  -上瘾入骨i
    2020-12-04 23:55

    yes, dynamic_cast for non-polymorphic types are not allowed. The base class shall have at least one virtual method. Only then that class can be called as polymorphic.

    This article explains a similar example: http://www.cplusplus.com/doc/tutorial/typecasting/

提交回复
热议问题