typeid(p1)
will give you the type of the pointer p1
, which will always be myclass1 *
(the string P8myclass1
is the mangled name for the type myclass1 *
). If you want the type of the pointed-at object, you want typeid(*p1)
, which should be myclass2
in this case.
With p2, typeid(p2)
will be myclass2 *
, while typeid(*p2)
gives undefined behavior -- you can't safely dereference a pointer after a static cast to the wrong type. It is likely that you'll get myclass1
, but not certain -- you might get a crash.
The debugger is essentially doing that with extra knowledge and protection to avoid bad misbehavior from the undefined behavior.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…