執行 期 型 態 訊息
C++
[编辑]雖然dynamic_cast
)、typeid
)
C++语言<typeinfo>
dynamic_cast
dynamic_cast<dest>(src);
dest
dynamic_cast
class base {
virtual ~base(){}
};
class derived : public base {
public:
virtual ~derived(){}
int compare (derived &ref);
};
int my_comparison_method_for_generic_sort (base &ref1, base &ref2)
{
derived & d = dynamic_cast<derived &>(ref1); // rtti used here
// rtti enables the process to throw a bad_cast exception
// if the cast is not successful
return d.compare (dynamic_cast<derived &>(ref2));
}
Visual C++
[编辑]struct TypeDescriptor
{
DWORD ptrToVTable;
DWORD spare;
char name[8];
};
struct PMD
{
int mdisp; //member displacement
int pdisp; //vbtable displacement
int vdisp; //displacement inside vbtable
};
struct RTTIBaseClassDescriptor
{
struct TypeDescriptor* pTypeDescriptor; //type descriptor of the class
DWORD numContainedBases; //number of nested classes following in the Base Class Array
struct PMD where; //pointer-to-member displacement info
DWORD attributes; //flags, usually 0
};
struct RTTIClassHierarchyDescriptor
{
DWORD signature; //always zero?
DWORD attributes; //bit 0 set = multiple inheritance, bit 1 set = virtual inheritance
DWORD numBaseClasses; //number of classes in pBaseClassArray
struct RTTIBaseClassArray* pBaseClassArray;
};
struct RTTICompleteObjectLocator
{
DWORD signature; //always zero ?
DWORD offset; //offset of this vtable in the complete class
DWORD cdOffset; //constructor displacement offset
struct TypeDescriptor* pTypeDescriptor; //TypeDescriptor of the complete class
struct RTTIClassHierarchyDescriptor* pClassDescriptor; //describes inheritance hierarchy
};