Does any one try the sample code on typeid, that post on MSDN(See
following)??
Seems it has something wrong to check with typeid operator,
the sample code is not going to work in my System...



cout << typeid( *pb ).name() << endl; //prints "class Derived"

this is the line causes exception....which is perfectly legal....
I have no idea why....

and its error message

First-chance exception at 0x77e4d756 in Test.exe: Microsoft C++ exception:
__non_rtti_object @ 0x0012fd78.


---------------------------------------
// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo.h>

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

class Derived : public Base {};

using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( *pd ).name() << endl; //prints "class Derived"
delete pd;
}
--------------------------------------------------------------------
Full Error Messages
----------------------------------------------------------------------
'Test.exe': Loaded 'J:\CProject\Test\Debug\Test.exe', Symbols loaded.
'Test.exe': Loaded 'G:\WINDOWS\system32\ntdll.dll', No symbols loaded.
'Test.exe': Loaded 'G:\WINDOWS\system32\kernel32.dll', No symbols loaded.
First-chance exception at 0x0046a7ce in Test.exe: 0xC0000005: Access
violation reading location 0x00000071.
First-chance exception at 0x77e4d756 in Test.exe: Microsoft C++ exception:
__non_rtti_object @ 0x0012fd8c.
Unhandled exception at 0x77e4d756 in Test.exe: Microsoft C++ exception:
__non_rtti_object @ 0x0012fd8c.

Re: About Casting in VC++ by Vincent

Vincent
Mon Aug 18 11:20:40 CDT 2003

Steven Lien wrote:
> Thanks....Someone mailed me and told me the tricks....
>
> Where i have to put /GR in C++ compiler...

You don't have to do that manually
There is a setting to enable RTTI

Project->Settings
C/C++ tab
C++ Language Category
Enable RTTI

> does it causes any resource comsume?....with that flag?

Yes
I can't remember how much but essentially it stores an extra flag per
type in your program so it can distinguish them.
Unless you have a huge number of classes it shouldn't matter too much

> RTTI can't be avoid in Data Strucuture,
> why VC++ must add /GR manually?

(If I am reading that correctly)
RTTI can be avoided.
It is only required for a small number of things
the most obvious being dynamic_cast<>

If you don't need it there is no need to use the extra memory it
requires, personally I have never needed it.

Normally if you find yourself relying on RTTI it means there is a flaw
in your design (not always, but normally).

Vin