typeid 与 RTTI什么关系,typeinfo是一个什么样的类,我这里有一段代码不知道什么意思
#include <typeinfo.h>
#include <iostream.h>
#include <string.h>
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 << "Unknown type! " << (typeid(*type)).name() << endl;
} void main()
{
graphicImage *gImage = new GIFimage();
graphicImage *pImage = new PICTimage();
processFile(gImage);
processFile(pImage);
}
#include <typeinfo.h>
#include <iostream.h>
#include <string.h>
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 << "Unknown type! " << (typeid(*type)).name() << endl;
} void main()
{
graphicImage *gImage = new GIFimage();
graphicImage *pImage = new PICTimage();
processFile(gImage);
processFile(pImage);
}
class Base { ... };class Derived : public Base { ... };void f()
{
Derived* pd = new Derived;
Base* pb = pd;
...
const type_info& t = typeid(pb); // t holds pointer type_info
const type_info& t1 = typeid(*pb); // t1 holds Derived info
...
}
是什么意思?