9
我預期這個代碼以打印 '同1' 和 'Same2',但它打印只有 'Same1':typeid的用於多態類型
#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";
}
兩個§ 5.2.8/2和5.3 §。 1/3似乎暗示我應該打印'Same2'。
什麼和在哪裏捕捉?
你是不是指'C c,* cr1 =&d'? – 2010-11-17 09:10:45
@dark_charlie:這基本上只是將操作員的地址移到兩行。即使你寫了'typeid((C *)&d)',你也會得到相同的結果。 – MSalters 2010-11-17 10:19:36
@ MSalters:啊,我是nm,我只是看不懂。 – 2010-11-17 11:25:26