RTTI的一个例子

深入浅出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. void main()
  67. {
  68.     graphicImage *gImage=new GIFimage();
  69.     graphicImage *pImage=new PICTimage();
  70.     processFile(gImage);
  71.     processFile(pImage);
  72. }

 

若在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.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值