1. 程式人生 > >c++ typeid 注意事項

c++ typeid 注意事項

typeid
對於不含有虛擬函式的類物件,無法識別
對於含有虛擬函式的物件能夠識別

無法準確識別任何指標物件

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class BaseNoVirtual
{
};

class DeriveNoVirtual
    :public BaseNoVirtual
{

};

class BaseVirtual
{
public:
    virtual int VirtualFn()
    {
        return
0; } }; class DeriveVirtual :public BaseVirtual { public: int VirtualFn() { return 1; } }; string WhoAreYou_PointNoVirtual(BaseNoVirtual* a_p) { return typeid(a_p).name(); } string WhoAreYou_NoVirtual(BaseNoVirtual* a_p) { return typeid(*a_p).name(); } string WhoAreYou_PointVirtual(BaseVirtual* a_p) { return
typeid(a_p).name(); } string WhoAreYou_Virtual(BaseVirtual* a_p) { return typeid(*a_p).name(); } int main() { auto bnv = BaseNoVirtual(); auto dnv = DeriveNoVirtual(); cout << "BaseNoVirtual 物件: " << typeid(bnv).name() << endl; cout << "DeriveNoVirtual 物件: "
<< typeid(dnv).name() << endl; auto pbnv = new BaseNoVirtual(); auto pdnv = new DeriveNoVirtual(); cout << "BaseNoVirtual 指標: " << typeid(pbnv).name() << endl; cout << "DeriveNoVirtual 指標: " << typeid(pdnv).name() << endl; cout << "BaseNoVirtual PVOID指標: " << typeid((void*)pbnv).name() << endl; cout << "DeriveNoVirtual PVOID指標: " << typeid((void*)pdnv).name() << endl; cout << "BaseNoVirtual 函式無虛基類指標: " << WhoAreYou_PointNoVirtual(pbnv) << endl; cout << "DeriveNoVirtual 函式無虛基類指標: " << WhoAreYou_PointNoVirtual(pdnv) << endl; cout << "BaseNoVirtual 函式無虛基類物件: "<< WhoAreYou_NoVirtual(pbnv) << endl; cout << "DeriveNoVirtual 函式無虛基類物件: " << WhoAreYou_NoVirtual(pdnv) << endl; cout << endl << endl << endl; auto bv = BaseVirtual(); auto dv = DeriveVirtual(); cout << "BaseVirtual 物件: " << typeid(bv).name() << endl; cout << "DeriveVirtual 物件: " << typeid(dv).name() << endl; auto pbv = new BaseVirtual(); auto pdv = new DeriveVirtual(); cout << "BaseVirtual 指標: " << typeid(pbv).name() << endl; cout << "DeriveVirtual 指標: " << typeid(pdv).name() << endl; cout << "BaseVirtual PVOID指標: " << typeid((void*)pbv).name() << endl; cout << "DeriveVirtual PVOID指標: " << typeid((void*)pdv).name() << endl; cout << "BaseVirtual 函式有虛基類指標: " << WhoAreYou_PointVirtual(pbv) << endl; cout << "DeriveVirtual 函式有虛基類指標: " << WhoAreYou_PointVirtual(pdv) << endl; cout << "BaseVirtual 函式有虛基類物件: " << WhoAreYou_Virtual(pbv) << endl; cout << "DeriveVirtual 函式有虛基類物件: " << WhoAreYou_Virtual(pdv) << endl; return 0; }

BaseNoVirtual 物件: class BaseNoVirtual
DeriveNoVirtual 物件: class DeriveNoVirtual
BaseNoVirtual 指標: class BaseNoVirtual * __ptr64
DeriveNoVirtual 指標: class DeriveNoVirtual * __ptr64
BaseNoVirtual PVOID指標: void * __ptr64
DeriveNoVirtual PVOID指標: void * __ptr64
BaseNoVirtual 函式無虛基類指標: class BaseNoVirtual * __ptr64
DeriveNoVirtual 函式無虛基類指標: class BaseNoVirtual * __ptr64
BaseNoVirtual 函式無虛基類物件: class BaseNoVirtual
DeriveNoVirtual 函式無虛基類物件: class BaseNoVirtual

BaseVirtual 物件: class BaseVirtual
DeriveVirtual 物件: class DeriveVirtual
BaseVirtual 指標: class BaseVirtual * __ptr64
DeriveVirtual 指標: class DeriveVirtual * __ptr64
BaseVirtual PVOID指標: void * __ptr64
DeriveVirtual PVOID指標: void * __ptr64
BaseVirtual 函式有虛基類指標: class BaseVirtual * __ptr64
DeriveVirtual 函式有虛基類指標: class BaseVirtual * __ptr64
BaseVirtual 函式有虛基類物件: class BaseVirtual
DeriveVirtual 函式有虛基類物件: class DeriveVirtual