RTTI的一个例子
来源:互联网 发布:知示的意思是什么 编辑:程序博客网 时间:2024/05/21 10:35
- //RTTI.CPP---built by cl -GR rtti.cpp <enter>
- #include <typeinfo.h>
- #include <iostream>
- #include <string.h>
- using namespace std;
- class graphicImage
- {
- protected:
- char name[80];
- public:
- graphicImage()
- {
- strcpy(name,"graphicImage");
- }
- virtual void display()
- {
- cout<<"Display a generic image."<<endl;
- }
- char * getName()
- {
- return name;
- }
- };
- //================================================
- class GIFimage : public graphicImage
- {
- public:
- GIFimage()
- {
- strcpy(name,"GIFimage");
- }
- void display()
- {
- cout<<"Display a GIF file."<<endl;
- }
- };
- /////////////////////////////////////////////
- class PICTimage: public graphicImage
- {
- public:
- PICTimage()
- {
- strcpy(name,"PICTimage");
- }
- void display()
- {
- cout<<"Display a PICT file."<<endl;
- }
- };
- ///////////////////////////////////////
- void processFile(graphicImage *type)
- {
- if(typeid(GIFimage)==typeid(*type) )
- {
- ((GIFimage*)type)->display();
- }
- else if(typeid(PICTimage)==typeid(*type))
- {
- ((PICTimage*)type)->display();
- }
- else
- cout<<"Unknow type!"<<(typeid(*type)).name()<<endl;
- }
- ////////////////////////////////
- void main()
- {
- graphicImage *gImage=new GIFimage();
- graphicImage *pImage=new PICTimage();
- processFile(gImage);
- processFile(pImage);
- }
若在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.
- RTTI的一个例子
- 我的一个获取 Form RTTI 信息的小例子
- 一个简单的RTTI实现
- 一个简单的RTTI实现
- C#中RTTI控制的例子
- 一个简单的C++的RTTI实现
- c++ RTTI例子
- vc2013 中 SystemC一个初级例子时产生错误error:"no RTTI data"
- 我的一个 RTTI 练习(参照 d5开发人员指南)
- 一个触发器的例子
- LineDDA的一个例子
- 一个函数的例子
- 一个触发器的例子
- DirectX的一个例子
- 一个Hibernate的例子
- ACE的一个例子
- 一个分页的例子
- 过滤的一个例子
- A Java Library of Graph Algorithms and Optimization (Discrete Mathematics and Its Applications)
- 四川一女教师被迫下跪向学生道歉后疯掉
- TypeAndRun--快速启动程序的工具
- Excel Hacks: Tips & Tools for Streamlining Your Spreadsheets
- CCNA: Cisco Certified Network Associate Study Guide: Exam 640-802
- RTTI的一个例子
- Rendering with mental ray & 3ds Max
- t46群
- Security in Distributed, Grid, Mobile, and Pervasive Computing
- SSH面视题
- Introduction to AutoCAD 2008: 2D and 3D Design
- 一个搞笑的Java程序
- Essential Silverlight: Getting started now with Microsofts new cross-platform plug-in for rich inte
- Excel 2007 Power Programming with VBA