typeid

class Base
{
public:
void vvfunc() {}
};

class Derived : public Base
{

};

int _tmain(int argc, _TCHAR* argv[])
{
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *" cout << typeid( pd ).name() << endl; //prints "class Derived *" try { // Base * pp = NULL; // cout << typeid( *pp ).name() << endl; cout << typeid( *pb ).name() << endl; //prints "class Derived" cout << typeid( *pd ).name() << endl; //prints "class Derived" } catch (std::__non_rtti_object e) { cout << "__non_rtti_object" << endl; } catch (std::bad_typeid e) { cout << "bad_typeid exception" << endl; } delete pd; return 0; }

Visual C++ .NET下,如果/GR 未打开,这个程序仍然成功打印.

但如果 public: virtual void vvfunc(), 则 typeid( *pb ), typeid( *pd )的会错误,抛出__non_rtti_object异常。

如果上面注释的两句打开,则会跑出__bad_typeid异常。

__non_rtti_object继承自__bad_typeid.