RTTI的一个例子

来源:互联网 发布:知示的意思是什么 编辑:程序博客网 时间:2024/05/21 10:35
深入浅出MFC的一个例子:
  1. //RTTI.CPP---built by cl -GR  rtti.cpp <enter>
  2. #include <typeinfo.h>
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6. class graphicImage
  7. {
  8.     protected:
  9.         char name[80];
  10.         public:
  11.             graphicImage()
  12.             {
  13.                 strcpy(name,"graphicImage");
  14.             }
  15.             
  16.             virtual void display()
  17.             {
  18.                 cout<<"Display a generic image."<<endl;
  19.             }
  20.             
  21.             char * getName()
  22.             {
  23.                 return name;
  24.             }
  25. };
  26. //================================================
  27. class GIFimage : public graphicImage
  28. {
  29.     public:
  30.         GIFimage()
  31.         {
  32.             strcpy(name,"GIFimage");
  33.         }
  34.     void display()
  35.     {
  36.         cout<<"Display a GIF file."<<endl;
  37.     }
  38. };
  39. /////////////////////////////////////////////
  40. class PICTimage: public graphicImage
  41. {
  42.     public:
  43.         PICTimage()
  44.         {
  45.             strcpy(name,"PICTimage");
  46.         }
  47.         void display()
  48.         {
  49.             cout<<"Display a PICT file."<<endl;
  50.         }
  51. };
  52. ///////////////////////////////////////
  53. void processFile(graphicImage *type)
  54. {
  55.     if(typeid(GIFimage)==typeid(*type) )
  56.     {
  57.       ((GIFimage*)type)->display();
  58.     }   
  59.     else if(typeid(PICTimage)==typeid(*type))
  60.     {
  61.         ((PICTimage*)type)->display();
  62.     }
  63.     else
  64.     cout<<"Unknow type!"<<(typeid(*type)).name()<<endl; 
  65. }
  66. ////////////////////////////////
  67. void main()
  68. {
  69.     graphicImage *gImage=new GIFimage();
  70.     graphicImage *pImage=new PICTimage();
  71.     processFile(gImage);
  72.     processFile(pImage);
  73. }

 

若在DOS 下,一切正常.

若在VC6.0 IDE环境下编译,出错.

f:/rtti.cpp(57) : warning C4541: 'typeid' used on polymorphic type 'class graphicImage' with /GR-; unpredictable behavior may result
查MS

Compiler Warning (level 1) C4541

'identifier' used on polymorphic type 'type' with /GR-; unpredictable behavior may result

You did not enable run-time type information and tried to use a feature that requires run-time type information support. Recompile with the /GR switch.

For more information, see the Enable Run-Time Type Information (/GR) compiler option.

 

 

/GR   (Enable Run-Time Type Information)

The Enable Run-Time Type Information option (/GR) causes the compiler to add code to check object types at run time. When this option is specified, the compiler defines the _CPPRTTI preprocessor macro. The option is cleared (/GR–) by default.

To find this option in the development environment, click Settings on the Project menu. Then click the C/C++ tab, and click C++ Language in the Category box.

For more information on run-time type checking, see Run-Time Type Information in the C++ Language Reference.