typeid for polymorphic types

后端 未结 1 1115
南方客
南方客 2020-12-20 16:17

I expected this code to print \'Same 1\' and \'Same2\', but it prints only \'Same1\':

#include 
#include 
using name         


        
相关标签:
1条回答
  • 2020-12-20 16:54

    Pointers aren't polymorphic types. They don't have virtual members. In fact, they have no members whatsoever. They also cannot derive from other types, nor be used as base classes. Hence, the static and dynamic type of a T* is always T*.

    In your "Same2" line, you're comparing the typeid of a pointer, not the pointed-to object. The compiler therefore only looks at the static types C* and D*. They're obviously not the same, and must have distinct type_info objects.

    0 讨论(0)
提交回复
热议问题