在C++中,当我们使用typeid获取对象或类的类型名称时,它将显示一个装饰(受损)字符串.我用cxxabi它去解码它:
#include <cxxabi.h>
#include <typeinfo>
namespace MyNamespace {
class MyBaseClass
{
public:
const std::string name()
{
int status;
char *realname = abi::__cxa_demangle(typeid (*this).name(),0,0, &status);
std::string n = realname;
free(realname);
return n;
}
};
}
int main()
{
MyNamespace::MyBaseClass h;
std::cout << h.name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
输出gcc是:
myNameSpace对象:: MyBaseClass
我需要MyNamespace::从上面的字符串中删除.我可以通过字符串操作删除它们 .
但是,有没有标准方法与cxxabi其他库或这样做或明确的解决方案?(至少可以在gcc和Visual C++之间移植)
没有标准的方法来执行此操作,期间,因为没有标准的方法来进行名称修改.如何表示名称是故意未指定的.C++标准中没有ABI.您正在使用的功能abi::__cxa_demangle是Itanium C++ ABI的一部分.该功能可能存在或可能不存在于其他地方
至于使用Itanium C++ ABI执行所需操作的方法,他们故意不提供此类功能.