`

typeinfo

    博客分类:
  • c++
阅读更多
typeid可以获得一个对象的动态类型名称
#include<iostream>
#include<vector>
#include<typeinfo>
using namespace std;

class Base{
public:
    virtual ~Base(){}
};

class Derived:public Base{
public:
    ~Derived(){}
};

int main()
{
    vector<Base*> v;
    for(int i=0;i<100;i++){
        v.push_back(new Base);
        v.push_back(new Derived);
    }
    for(vector<Base*>::const_iterator iter = v.begin();
                                      iter != v.end();
                                      ++iter)
                                      {
        if(Derived* d = dynamic_cast<Derived*>(*iter)){
            cout << typeid(d).name() << endl;
        }else{
            cout << typeid(*iter).name() << endl;
        }
    }
}

P4Base
P7Derived
P4Base
P7Derived
...


#include<iostream>
#include<typeinfo>
#include<string>
using namespace std;

template<typename T>
void print(const T& t)
{
    cout << "typeid:" << typeid(t).name() << endl;
}

int main()
{
    int a = 0;
    float b = 1;
    double c = 2;
    string d = "3";
    print(a);
    print(b);
    print(c);
    print(d);
}

typeid:i
typeid:f
typeid:d
typeid:Ss


type_info::before来sort
#include<stdlib.h>
#include<vector>
#include<iostream>
#include<typeinfo>
#include<map>

using namespace std;

class Shape{
public:
    virtual void draw() = 0;
    virtual ~Shape(){};
    virtual int edge(){return 3;}
};

class Circle:public Shape{
public:
    void draw(){cout << "Circle::draw()"<<endl; }
    int edge(){return 3;}
    ~Circle(){cout << "~Circle()" << endl; }
};

class Rectangle : public Shape{
public:
    void draw(){ cout << "Rectangle::draw()" << endl;}
    int edge(){return 4;}
    ~Rectangle(){cout << "~Rectangle()" << endl;}
};

class Square : public Shape{
public:
    void draw(){ cout << "Square::draw()" << endl;}
    int edge(){return 4;}
    ~Square(){ cout << "~Square()" << endl;}
};

void drawQuad(Shape& shape){
    if(shape.edge()<4){
        cout << "not a polygone" << endl;
    }else{
        cout << "a polygone" << endl;
    }
}

struct TInfoLess{
    bool operator()(const type_info* t1,const type_info* t2)const{
        return t1->before(*t2);
    }
};

typedef map<const type_info*,vector<Shape*>,TInfoLess> shapeMap;

int main(){
    srand(time(0));//seed a random
    vector<Shape*> v;
    shapeMap smap;
    int num = 0;
    Shape *shape;
    for(int i=0;i<10;++i){
        num = rand();
        switch(num%3){
        case 0:
            shape = new Circle;
            break;
        case 1:
            shape = new Rectangle;
            break;
        case 2:
            shape = new Square;
            break;
        }
        smap[&typeid(*shape)].push_back(shape);
    }
    for(shapeMap::iterator iter = smap.begin();iter!=smap.end();++iter){
        //print
    }
}


分享到:
评论

相关推荐

    C语言头文件 TYPEINFO.H

    C语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC语言头文件 TYPEINFO.HC...

    typeinfo.pets的jar包(第四版 thinking in java)

    在java编程思想一书中经常需要用到的jar包,typeinfo.pets

    typeinfo-pets.jar Java编程思想所需工具包.rar

    软件介绍: typeinfo-pets.jar是Java编程思想第四版中用源码生成的jar文件所需使用的jar包,解压后自行添加到Bulid path路径里即可使用。

    typeinfo.pets.jar

    think java中需要用到的包

    C语言头文件 TYPEINFO

    C语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 TYPEINFOC语言头文件 ...

    private_typeinfo.rar_DST_it

    pointer to a dst_type which has (static_ptr, static_type) above it const void dst_ptr_leading_to_static_ptr .

    C++头文件大全.pdf

    类型识别:typeinfo 智能指针:memory 输入输出流操作:iomanip、sstream 字符处理:cctype、cwctype 局部化:locale 这只是一小部分C++标准库头文件,实际上C++标准库还有更多的头文件,涵盖了各种不同的功能和特性...

    高效实用的C 委托库,支持普通函数,仿函数.rar

     只使用了STL的vector和typeinfo库.性能与boost的function相同,性能与boost::function库相同。  利用[](...)调用单个函数委托性能稍好一些。  二、声明形式:  目前支持0到9个参数  QLib::Event ev0; //零...

    C++标准库stl

    &lt;typeinfo&gt; 支持变量在运行期间的类型标识 &lt;exception&gt; 支持异常处理,这是处理程序中可能发生的错误的一种方式 &lt;cstdarg&gt; 支持接受数量可变的参数的函数。即在调用函数时,可以给函数传送数量不等的数据项。它...

    Delphi 调用WebService 出错

    InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherWebServiceSoap), ioDocument); 把这句加上2005前的版本不加这个就出错,我也是找了很久才发现的 而且要加到initialization 最后一行,我加到initialization ...

    远古仿迅雷看看模板(新)下载

    打开企业管理器-&gt;数据库-&gt;WebMedia-&gt;表-&gt;TypeInfo-&gt;打开表-&gt;返回所有行-&gt;建立好您电影分类名称 打开企业管理器-&gt;数据库-&gt;WebMedia-&gt;视图-&gt;v_Proginfo-&gt;设置视图,找到wherefrom、dtPhoto、 ProgNotes 前面勾上保存...

    JSON还原为结构体

    RecordLoadJSON(gloss, @json[1], TypeInfo(TGlossary)); Memo1.Clear; Memo1.Lines.Add(gloss.glossary.title); // 中国 Memo1.Lines.Add(gloss.glossary.GlossDiv.title); // 湖南省 Memo1.Lines.Add(gloss....

    think in java 编程思想包

    think in java这本书上的源代码,免除了新手你敲代码麻烦,方便你的使用和学习

    vb写的ole 回调函数

    'OLE typeinfo data in the system registry. After running it once, close the server 'manually and everything should be set for your client app to call the server through 'OLE. 'Note6: Every time you ...

    C标准库源代码(学习C/C++必备)

    介绍说明 C标准库源代码,能提高对C的理解,不错的哦 下载文件列表 Pack : clibsource.rar C 标准库源代码\ABORT.C C标准库源代码\ABS.C C标准库源代码\ACCESS.C C标准库源代码\ADJUSTFD.C C标准库源代码\ALGRITHM ...

    Thinking_in_C++_中文版【高清+可搜索+可编辑/复制/粘贴+无密码】

    #include &lt;typeinfo.h&gt; #include"·.\14\tstash.h" class shape { protected: S 七atic in七coun店 public: shape() { count++; vir七ual -shape() = O { count--; } vir七ual void draw () const = O; static in七...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    SetEnumType(TypeInfo('TMyDelphiEnumType')); - ADD: Added the functions PathLength and PathLengthPos in the module FlexPath. Also added the parameter LengthPos in the function PointOnLine (see the ...

    运行时:一个Swift运行时库,用于查看类型信息以及动态获取和设置属性

    TypeInfo公开有关本机Swift结构,协议,类,元组和枚举的元数据。 它捕获属性,通用类型,继承级别等。 例 假设您有一个用户结构: struct User { let id : Int let username : String let email : String } 要...

    class--:将 C++ 类转换为普通 C 结构以与 IDA ghidra 本地类型一起使用

    将 ctags-Universal JSON 输出传递到该程序中,以从 C++ 头文件中解析出类/结构(例如 cmd&gt;python classesstripper.py typeinfo.json)重要说明:告诉 ctags 不要排序以保持成员变量的顺序正确示例 cmd 行:ctags -...

Global site tag (gtag.js) - Google Analytics