做赚钱的网站有哪些,专业网站制作哪家强,手机怎么访问wap网站,河南洛阳网络公司给定一个含有虚函数的对象的地址#xff0c;找到对应的类名#xff0c;不同平台下方法也不同#xff0c;这是由于RTTI实现并没有统一的标准。
Linux#xff1a; #include iostream
#include typeinfoclass Person
{
public:virtual void func(){std::cout…给定一个含有虚函数的对象的地址找到对应的类名不同平台下方法也不同这是由于RTTI实现并没有统一的标准。
Linux #include iostream
#include typeinfoclass Person
{
public:virtual void func(){std::cout typeid(*this).name() std::endl;}
};const char* ClassName(void* address)
{uintptr_t* objPtr reinterpret_castuintptr_t*(address);uintptr_t* vftablePtr reinterpret_castuintptr_t*(*objPtr);std::type_info* ti reinterpret_caststd::type_info*(*(vftablePtr - 1));return ti-name();
}int main()
{Person p;std::cout ClassName(p) std::endl;std::cout typeid(p).name() std::endl;
}运行结果
6Person
6Person其中Person代表类名6表示的是类名的长度。 Windows x86 #include iostream
#include typeinfoclass Person
{
public:virtual void func(){std::cout typeid(*this).name() std::endl;}
};const char* ClassMangledName(void* address)
{uintptr_t* objPtr reinterpret_castuintptr_t*(address);uintptr_t* vftablePtr reinterpret_castuintptr_t*(*objPtr);uintptr_t* rttiObject reinterpret_castuintptr_t*(*(vftablePtr - 1));std::type_info* ti reinterpret_caststd::type_info*(*(rttiObject 3));const char* className ti-name();return className;
}int main()
{Person p;std::cout ClassMangledName(p) std::endl;std::cout typeid(p).name() std::endl;
}运行结果
class Person
class PersonWindows x64 #include iostream
#include ehdata.h
#include rttidata.hclass Person
{
public:virtual void func(){std::cout typeid(*this).name() std::endl;}
};const char* ClassMangledName(void* address)
{uintptr_t* objPtr reinterpret_castuintptr_t*(address);uintptr_t* vftablePtr reinterpret_castuintptr_t*(*objPtr);_RTTICompleteObjectLocator* rttiLocator reinterpret_cast_RTTICompleteObjectLocator*(*(vftablePtr - 1));uintptr_t imageBase reinterpret_castuintptr_t(rttiLocator) - rttiLocator-pSelf;TypeDescriptor* typeDescriptor reinterpret_castTypeDescriptor*(imageBase rttiLocator-pTypeDescriptor);const char* classMangledName typeDescriptor-name;return classMangledName;
}int main()
{Person p;std::cout ClassMangledName(p) std::endl;std::cout typeid(p).raw_name() std::endl;
}运行结果
.?AVPerson
.?AVPerson打印的结果是raw name也就是未修饰的名字。