我正在运行GCC C++编译器上的代码,以输出type_info::name:
#include <iostream>
#include <typeinfo>
using namespace std;
class shape {
protected:
int color;
public:
virtual void draw() = 0;
};
class Circle: public shape {
protected:
int color;
public:
Circle(int a = 0): color(a) {};
void draw();
};
void Circle::draw() {
cout<<"color: "<<color<<'\n';
}
class triangle: public shape {
protected:
int color;
public:
triangle(int a = 0): color(a) {};
void draw();
};
void triangle::draw() {
cout<<"color: "<<color<<'\n';
}
int main() {
Circle* a;
triangle* b;
cout<<typeid(a).name()<<'\n';
cout<<typeid(b).name()<<'\n';
}但我得到了以下结果:
P6Circle
P8triangle对恶魔来说,
./shape | c++filt 我得到了和之前一样的输出。还有其他解决办法吗?
发布于 2013-09-25 13:06:33
您需要对类型使用c++filt -t,所以下面的内容应该可以工作:
./shape | c++filt -tc++filt手册页为-t提供了以下内容
尝试拆分类型和函数名。默认情况下,这是禁用的,因为损坏的类型通常只在编译器内部使用,并且它们可以与非损坏的名称混淆。例如,一个称为"a“的函数被看作是一个错误的类型名称,它将被定义为"signed”。
发布于 2013-09-25 13:08:30
您使用的是GCC的哪个版本(及其相应的libstdc++)?
GCC 4.8,我有
static inline std::string
demangled_type_info_name(const std::type_info&ti)
{
int status = 0;
return abi::__cxa_demangle(ti.name(),0,0,&status);
}然后我就可以用
std::cout << demangled_type_info_name(typeid(*ptr)) << std::endl;其中,ptr指向具有RTTI的对象(即使用一些虚拟方法,特别是虚拟析构函数)。
https://stackoverflow.com/questions/19005744
复制相似问题