相关疑难解决方法(0)

typeid用于多态类型

我希望这段代码能够打印'Same 1' 'Same2',但它只打印'Same1':

#include <iostream>
#include <typeinfo>
using namespace std;

struct C{virtual ~C(){}};
struct D : C{};
int main(){
   D d;
   C c, &cr1 = d;
   if(typeid(cr1) == typeid(D)) cout << "Same1";
   if(typeid(&cr1) == typeid(D*)) cout << "Same2";
}
Run Code Online (Sandbox Code Playgroud)

§5.2.8/ 2和§5.3.1/ 3似乎都向我建议应该打印'Same2'.

捕获的内容和位置是什么?

c++ typeid

9
推荐指数
1
解决办法
2134
查看次数

标签 统计

c++ ×1

typeid ×1